Jump to content

Warning: Undefined variable $imageSource


EarthDay

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.