Eaten Taik Eaten Taik -4 years ago 76
PHP Question

Inserting image to temporary file

I have done my coding that inserting images to temporary file. I am able to upload images with size below 5mb but some of images unable to be inserted and I wonder why. Can somebody tell me what I done wrong in my coding below?

$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.";
}


EDIT

I found out that images which size
> 2MB
unable to be moved to the temporary file but it pass the line
if($imgSize < 5000000)
. How do I fix this?

Answer Source

I found out that this is caused by the setting in php.ini where upload_max_filesize set to 2M. I hope this will be useful to others that facing the same problem as mine. Go to your php.ini and see the File Uploads section and find the line upload_max_filesize = 2M . Change the 2M to any size that meeting with your requirement and restart all the services :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download