simona6 Posted March 10, 2020 Share Posted March 10, 2020 adding photo,Array ( [name] => profilepic.jpeg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\phpB923.tmp [error] => 0 [size] => 152127 ) Hi, We have been using this code for ages, but suddenly for some reason we cannot upload an image using the code below. When we try to Print_r the $profilephoto variable, I get this error. The filename is correct, but what is that [error]?? And how do I resolve it? Oddly it does upload it locally, but LIVE, it won't This is the code. if (isset($updatephoto)) { echo "adding photo,"; print_r($profilephoto); define ("MAX_SIZE","5000"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if($_SERVER["REQUEST_METHOD"] == "POST") { $image =$_FILES["profilephoto"]["name"]; $uploadedfile = $_FILES['profilephoto']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['profilephoto']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo "Unknown Extension..!"; } else { $size=filesize($_FILES['profilephoto']['tmp_name']); if ($size > MAX_SIZE*1024) { echo "File Size Excedeed..!!"; } if($extension=="jpg" || $extension=="jpeg" ) { $uploadedfile = $_FILES['profilephoto']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if($extension=="png") { $uploadedfile = $_FILES['profilephoto']['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); echo $scr; } list($width,$height)=getimagesize($uploadedfile); $newwidth=600; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); $pic=($_FILES['profilephoto']['name']); $random = (rand()%99999999); $newname="$random"."$pic"; $filename = "images/profiles/". $newname; imagejpeg($tmp,$filename,100); imagedestroy($src); imagedestroy($tmp); }} } $query = ("UPDATE users SET profilephoto =:newname WHERE id =:userid"); $result = $pdo->prepare($query); $result->execute(array(':userid' => $userid, ':newname' => $newname)); echo "<script> window.location.replace('/profile/') </script>";} Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/ Share on other sites More sharing options...
ginerjm Posted March 10, 2020 Share Posted March 10, 2020 I don't see an error message. Have you added any echos to be sure things are going the way you think they are? Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575342 Share on other sites More sharing options...
mac_gyver Posted March 10, 2020 Share Posted March 10, 2020 31 minutes ago, simona6 said: [error] => 0 if you are asking about the value zero in the ['error'] element, that means the upload was successful, which you would know by making use of the php.net documentation on uploading files. your existing code should be testing for that value before using any of the uploaded file information and reporting back to the user that the upload failed for any of the other possible values. for the posted print_r() output, if the code 'isn't working', the problem is something else. do you have php's error related settings setup so that all php errors will get displayed or logged? Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575343 Share on other sites More sharing options...
simona6 Posted March 10, 2020 Author Share Posted March 10, 2020 Yes errors are turned on, but there aren't any. Only those I sent here in the Error Log file. This is an example of the errors, and they are pretty much all like this: mod_fcgid: stderr: PHP Notice: Undefined variable: profilephoto in /var/www/vhosts/site.net/httpdocs/includes/profile.inc on line 253, referer: https://www.site.net/profile Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575345 Share on other sites More sharing options...
simona6 Posted March 10, 2020 Author Share Posted March 10, 2020 <form method='POST' action='' name='signuprocess' enctype='multipart/form-data'> <input type='hidden' name='updatephoto' value='update'><div class='profile-photo'><h2><div class='profile-icon'><i class='fa fa-camera'></i></div> Profile Photo</h2><br/><div class='signup signup-title'><p><b>Please upload a photo of yourself, not your family.</p></div><div class='signup form-field'><input name='profilephoto' type='file' size='15' /></div> <input type='submit' value='Upload'></form> This is the form. It was working up until a few days ago, so I am suspicious of something changing on the server, through some auto update. It uploads the file fine, when running locally. Both are on PHP 7.2. Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575347 Share on other sites More sharing options...
ginerjm Posted March 10, 2020 Share Posted March 10, 2020 I would definitely fix those errors before proceeding. Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575349 Share on other sites More sharing options...
simona6 Posted March 10, 2020 Author Share Posted March 10, 2020 What errors? If you mean the one I have posted, I don't know what the fix is, and that is the issue here. Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575350 Share on other sites More sharing options...
ginerjm Posted March 10, 2020 Share Posted March 10, 2020 They say 'undefined variable'. So - define them. If nothing else, simply set them to (quote)(quote), ie an empty value. Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575351 Share on other sites More sharing options...
simona6 Posted March 10, 2020 Author Share Posted March 10, 2020 if (isset($updatephoto)) { define ("MAX_SIZE","5000"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if($_SERVER["REQUEST_METHOD"] == "POST") { $image = $_FILES["profilephoto"]["name"]; $uploadedfile = $_FILES['profilephoto']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['profilephoto']['name']); $extension = getExtension($filename); $extension = strtolower($extension); echo "test here $uploadedfile"; If I add the final line in this grab of the code, it does not show anything on screen. So it's as if $_FILES["profilephoto"]["name"] just isn't being seen. Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575352 Share on other sites More sharing options...
simona6 Posted March 10, 2020 Author Share Posted March 10, 2020 Resolved. It was the server setting in PHP. The Memory was left blank, and the File Uploads was on OFF for some bizarre reason. An update must have set those 'back end'. Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575353 Share on other sites More sharing options...
ginerjm Posted March 10, 2020 Share Posted March 10, 2020 (edited) Glad you solved it. BTW - Here is a more 'proper' way to structure your code: define ("MAX_SIZE","5000"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $ext = substr($str,$i+1); return $ext; } //******** Begin main body ************ if (isset($updatephoto)) { $errors = 0; if($_SERVER["REQUEST_METHOD"] == "POST") { $image = $_FILES["profilephoto"]["name"]; $uploadedfile = $_FILES['profilephoto']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['profilephoto']['name']); $extension = getExtension($filename); $extension = strtolower($extension); echo "test here $uploadedfile"; } } } Note: you don't bury a function def inside your logic. Of course for a script with multiple functions, I keep them at the end so that I don't have to wade thru them constantly when editing the script. Edited March 10, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575354 Share on other sites More sharing options...
semidotinfotech Posted March 14, 2020 Share Posted March 14, 2020 On 3/10/2020 at 8:29 PM, simona6 said: adding photo,Array ( [name] => profilepic.jpeg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\phpB923.tmp [error] => 0 [size] => 152127 ) Hi, We have been using this code for ages, but suddenly for some reason we cannot upload an image using the code below. When we try to Print_r the $profilephoto variable, I get this error. The filename is correct, but what is that [error]?? And how do I resolve it? Oddly it does upload it locally, but LIVE, it won't This is the code. if (isset($updatephoto)) { echo "adding photo,"; print_r($profilephoto); define ("MAX_SIZE","5000"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if($_SERVER["REQUEST_METHOD"] == "POST") { $image =$_FILES["profilephoto"]["name"]; $uploadedfile = $_FILES['profilephoto']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['profilephoto']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo "Unknown Extension..!"; } else { $size=filesize($_FILES['profilephoto']['tmp_name']); if ($size > MAX_SIZE*1024) { echo "File Size Excedeed..!!"; } if($extension=="jpg" || $extension=="jpeg" ) { $uploadedfile = $_FILES['profilephoto']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if($extension=="png") { $uploadedfile = $_FILES['profilephoto']['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); echo $scr; } list($width,$height)=getimagesize($uploadedfile); $newwidth=600; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); $pic=($_FILES['profilephoto']['name']); $random = (rand()%99999999); $newname="$random"."$pic"; $filename = "images/profiles/". $newname; imagejpeg($tmp,$filename,100); imagedestroy($src); imagedestroy($tmp); }} } $query = ("UPDATE users SET profilephoto =:newname WHERE id =:userid"); $result = $pdo->prepare($query); $result->execute(array(':userid' => $userid, ':newname' => $newname)); echo "<script> window.location.replace('/profile/') </script>";} I think you have updated your PHP version so you need to check your server setting. Quote Link to comment https://forums.phpfreaks.com/topic/310265-photo-uploading-failing/#findComment-1575432 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.