drisate Posted September 29, 2015 Share Posted September 29, 2015 I need to upload 3 images and resize them as part of a forme but for some reason, the images does not end up in the server directory. What am i missing? The fille array looks like this: Array ( [file1] => Array ( [name] => s_casafolio.png [type] => image/png [tmp_name] => /tmp/phpENy7j4 [error] => 0 [size] => 2047496 ) [file2] => Array ( [name] => lawjournal.png [type] => image/png [tmp_name] => /tmp/phpT0fu1Y [error] => 0 [size] => 1139448 ) [file3] => Array ( [name] => s_mdj_2.png [type] => image/png [tmp_name] => /tmp/phpUsRk6W [error] => 0 [size] => 792873 ) ) And my processing looks like this: <?php $tmp = md5(md5(date("YmdHis"))); $img_quality = 70; $maxDim = 700; $dest_folder = "/home/user/public_html/userfiles/inscription_designer/"; $valid_mime_types_images = array( "image/gif", "image/png", "image/jpeg", "image/pjpeg", ); if (in_array($_FILES["file1"]["type"], $valid_mime_types_images)) { $ext = pathinfo($_FILES['file1']['name'], PATHINFO_EXTENSION); $dest_file = $dest_folder.$tmp.'1.'.$ext; $fn = $_FILES['file1']['tmp_name']; $size = getimagesize($fn); $ratio = $size[0]/$size[1]; // width/height if( $ratio > 1) { $width = $maxDim; $height = $maxDim/$ratio; } else { $width = $maxDim*$ratio; $height = $maxDim; } $src = imagecreatefromstring(file_get_contents($fn)); $dst = imagecreatetruecolor($width,$height); imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); imagedestroy($src); imagepng($dst,$dest_file); // adjust format as needed imagedestroy($dst); }else{ $err .= "- Image 1 est invalide.<br />"; } if (in_array($_FILES["file2"]["type"], $valid_mime_types_images)) { $ext = pathinfo($_FILES['file2']['name'], PATHINFO_EXTENSION); $dest_file = $dest_folder.$tmp.'2.'.$ext; $fn = $_FILES['file2']['tmp_name']; $size = getimagesize($fn); $ratio = $size[0]/$size[1]; // width/height if( $ratio > 1) { $width = $maxDim; $height = $maxDim/$ratio; } else { $width = $maxDim*$ratio; $height = $maxDim; } $src = imagecreatefromstring(file_get_contents($fn)); $dst = imagecreatetruecolor($width,$height); imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); imagedestroy($src); imagepng($dst,$dest_file); // adjust format as needed imagedestroy($dst); }else{ $err .= "- Image 2 est invalide.<br />"; } if (in_array($_FILES["file3"]["type"], $valid_mime_types_images)) { $ext = pathinfo($_FILES['file3']['name'], PATHINFO_EXTENSION); $dest_file = $dest_folder.$tmp.'3.'.$ext; $fn = $_FILES['file3']['tmp_name']; $size = getimagesize($fn); $ratio = $size[0]/$size[1]; // width/height if( $ratio > 1) { $width = $maxDim; $height = $maxDim/$ratio; } else { $width = $maxDim*$ratio; $height = $maxDim; } $src = imagecreatefromstring(file_get_contents($fn)); $dst = imagecreatetruecolor($width,$height); imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); imagedestroy($src); imagepng($dst,$dest_file); // adjust format as needed imagedestroy($dst); }else{ $err .= "- Image 3 est invalide.<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/298368-php-image-upload-ans-resize/ Share on other sites More sharing options...
Stefany93 Posted September 29, 2015 Share Posted September 29, 2015 You need to use move_uploaded_file -> http://php.net/manual/bg/function.move-uploaded-file.php Quote Link to comment https://forums.phpfreaks.com/topic/298368-php-image-upload-ans-resize/#findComment-1521897 Share on other sites More sharing options...
drisate Posted September 29, 2015 Author Share Posted September 29, 2015 Normaly "imagepng($dst,$dest_file);" replaces that and avoids uploading user content to the server in case the file is infected Quote Link to comment https://forums.phpfreaks.com/topic/298368-php-image-upload-ans-resize/#findComment-1521898 Share on other sites More sharing options...
Stefany93 Posted September 29, 2015 Share Posted September 29, 2015 Yes, but it is not working here, obviously. Either change the function, OR look in the PHP.ini whether you have file uploads allowed. Also check the $_POST vars max size in php.ini Some hosts has those settings turned off by default. Quote Link to comment https://forums.phpfreaks.com/topic/298368-php-image-upload-ans-resize/#findComment-1521899 Share on other sites More sharing options...
Jacques1 Posted September 29, 2015 Share Posted September 29, 2015 (edited) The problem could be pretty much anything, maybe an application bug, maybe an issue with the PHP settings, maybe incorrect permissions on the server. So you'll have to narrow it down yourself. Check the error log (if it's disabled, turn it on now and try again). What do you see? You also need to learn about using functions. Copy-pasting of large bocks of code is not how programming works and will make debugging much harder. Edited September 29, 2015 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/298368-php-image-upload-ans-resize/#findComment-1521901 Share on other sites More sharing options...
Jacques1 Posted September 29, 2015 Share Posted September 29, 2015 The code is working fine on my machine, so this looks more like a permission/configuration issue. Quote Link to comment https://forums.phpfreaks.com/topic/298368-php-image-upload-ans-resize/#findComment-1521904 Share on other sites More sharing options...
Stefany93 Posted September 29, 2015 Share Posted September 29, 2015 The problem could be pretty much anything, maybe an application bug, maybe an issue with the PHP settings, maybe incorrect permissions on the server. So you'll have to narrow it down yourself. Check the error log (if it's disabled, turn it on now and try again). What do you see? You also need to learn about using functions. Copy-pasting of large bocks of code is not how programming works and will make debugging much harder. Yes, break down the various functions into modules, that makes life way easier. Quote Link to comment https://forums.phpfreaks.com/topic/298368-php-image-upload-ans-resize/#findComment-1521906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.