Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to store images to database which is moving the image to temporary file first then only save the image name to mysql database. I am able to upload image but some of image which size is > 2MB unable to move to temporary files. I am not sure either it is because of granting permissions to the files since I saw some forum in the internet bring up this matter when others having a problem moving files to tmp file.

What I have tried:

Here is coding that I currently use.

PHP
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];

$upload_dir = 'user_images/'; // upload directory

$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

// valid image extensions
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions

// rename uploading image
$userpic = rand(1000,1000000).".".$imgExt;

// allow valid image file formats
if(in_array($imgExt, $valid_extensions)){           
    // Check file size '5MB'
    if($imgSize < 5000000)              {
        move_uploaded_file($tmp_dir,$upload_dir.$userpic);
    }
    else{
        $errMSG = "Sorry, your file is too large.";
    }
}
else{
    $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";        
}
Posted
Updated 1-Mar-17 3:41am

You should check for file upload errors before trying to move the file. See PHP: Error Messages Explained - Manual[^].
 
Share this answer
 
Most likely it is constrained by these default settings in the php.ini file:
upload_max_filesize[^]
and
post_max_size[^]
 
Share this answer
 
Comments
dell-gl62m 1-Mar-17 23:52pm    
I found it out and you are correct. It's about the setting in the php.ini

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900