EarthDay Posted August 25, 2022 Share Posted August 25, 2022 Hi there, I have just upgraded to PHP8 on my test server a few days ago and I am testing the referral form and it's giving me the below error message when submitting with or without an image inn the upload box; "Warning: Undefined variable $imageSource in /var/www/vhosts/site/form/index.php on line 119" I have checked my variables and as far as I understand (still learning PHP) have been set correctly. I have also read some other articles on here but nothing seems to work :( The images no longer upload. This was working fine in PHP 7. if (!empty($_POST)) { $folder ="uploads/"; $image = $_FILES['image']['name']; $path = $folder . uniqid().$image ; $target_file=$folder.basename($_FILES["image"]["name"]); $imageFileType=pathinfo($target_file,PATHINFO_EXTENSION); $allowed=array('jpeg','png' ,'jpg','gif'); $filename=$_FILES['image']['name']; $ext=pathinfo($filename, PATHINFO_EXTENSION); if(!in_array($ext,$allowed) ) { $msg = "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; } else{ move_uploaded_file( $_FILES['image'] ['tmp_name'], $path); $imageSource = $path; } Line 119; $image = $imageSource; Cheers, ED. Quote Link to comment https://forums.phpfreaks.com/topic/315236-warning-undefined-variable-imagesource/ Share on other sites More sharing options...
gw1500se Posted August 25, 2022 Share Posted August 25, 2022 If your conditional fails then that variable is not set. You only set it in the else clause. You need to initialize it to something or test if it exists before using it. Quote Link to comment https://forums.phpfreaks.com/topic/315236-warning-undefined-variable-imagesource/#findComment-1599820 Share on other sites More sharing options...
Barand Posted August 25, 2022 Share Posted August 25, 2022 Your definition is conditional. It only gets defined if the "else" path is followed. Define it as an empty string before the if(). Quote Link to comment https://forums.phpfreaks.com/topic/315236-warning-undefined-variable-imagesource/#findComment-1599821 Share on other sites More sharing options...
Barand Posted August 25, 2022 Share Posted August 25, 2022 Damn! Too slow, again. Quote Link to comment https://forums.phpfreaks.com/topic/315236-warning-undefined-variable-imagesource/#findComment-1599822 Share on other sites More sharing options...
mac_gyver Posted August 25, 2022 Share Posted August 25, 2022 you actually need to find out why it is not defined, why that conditional code is not being executed, not just attempt to make the undefined error go-away. your logic requires $_POST to be not empty and the file extension to pass the in_array() call. for debugging use print_r(), before the existing !empty($_POST) conditional statement, on one or both $_POST/$_FILES to see what they are. if the total size of the form data exceeds the post_max_size setting, both the $_POST and $_FILES arrays will be empty. your form processing code MUST first detect if a post method form was submitted, then detect if there is or is not $_POST/$_FILES data, before using any of the uploaded file information. if there is not, you need to setup a message for the user that the total size of the form data is too large and could not be processed. another server-side reason why uploads can fail, of any size, is if uploads are not enabled on the server. Quote Link to comment https://forums.phpfreaks.com/topic/315236-warning-undefined-variable-imagesource/#findComment-1599824 Share on other sites More sharing options...
mac_gyver Posted August 25, 2022 Share Posted August 25, 2022 3 hours ago, EarthDay said: submitting with or without an image inn the upload box; continuation from above, after you have determined that there is data in $_FILES, you must test the ['error'] element. a zero (UPLOAD_ERR_OK) means that the file was successfully upload and the data in the other elements can be used. a 4 (UPLOAD_ERR_NO_FILE) means that no file was selected. for the other possible errors, which are listed in the php documentation, some are under the control of the user and some are not. for those that the user can affect, you need to setup a specific message telling the user what is wrong. for the other errors, setup a general failure message and log all the information about the actual error that occurred, so that you can find and fix what's causing the problem. Quote Link to comment https://forums.phpfreaks.com/topic/315236-warning-undefined-variable-imagesource/#findComment-1599825 Share on other sites More sharing options...
EarthDay Posted September 2, 2022 Author Share Posted September 2, 2022 On 8/25/2022 at 8:19 PM, mac_gyver said: continuation from above, after you have determined that there is data in $_FILES, you must test the ['error'] element. a zero (UPLOAD_ERR_OK) means that the file was successfully upload and the data in the other elements can be used. a 4 (UPLOAD_ERR_NO_FILE) means that no file was selected. for the other possible errors, which are listed in the php documentation, some are under the control of the user and some are not. for those that the user can affect, you need to setup a specific message telling the user what is wrong. for the other errors, setup a general failure message and log all the information about the actual error that occurred, so that you can find and fix what's causing the problem. Hi there, So sorry for the late reply, I have had a week off from work and just got back today. Thank you all so much for the information, I have solved this by removing the else{ statement and changed the insert query to $path and works fine. Cheers, ED. Quote Link to comment https://forums.phpfreaks.com/topic/315236-warning-undefined-variable-imagesource/#findComment-1599993 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.