How to retrieve a file name?

I am trying to create code that will:

  1. Allow the user to select a file from their local system.
  2. Save the file’s name as “icon_imagename” in my “tableIcons” table on a MySQL database
  3. Save the file into the “images/icons” directory on my web server. (I do not want to save the image into the data base – only the name).

Here’s the code I am using for the user to select the file:

$uploadNewImage = $_POST['uploadNewImage'];

echo '<form enctype="multipart/form-data" action="main-icons.php?id=u' . $userSelection .'" method="post">';
        echo '<input type="file" name="uploadNewImage" id="uploadNewImage"><br>';
        echo '<input type="hidden" name="switch_upload" value="y">';
        echo '<input type="submit" value="Upload Image" name="submitImage">';
echo '</form>';

Here’s the code I have for saving the file onto the webserver (which is working fine):

move_uploaded_file($_FILES["uploadNewImage"]["tmp_name"], $target_dir . basename($_FILES["uploadNewImage"]["name"])); 

Here’s the current code I have for saving the file name into the database (which is not working):

$sql_add = 'INSERT INTO tableIcons (icon_imagename) VALUES (:iconImageName);';
$pdo->prepare($sql_add)->execute(['iconImageName'=>$uploadNewImage]);

Can anyone see the reason why the image name is not being added to the database? I’m not getting an error message when I submit the form, however the image name is not being added.

I have also tried amending my insert code to this, but it is not working:

$sql_add = 'INSERT INTO tableIcons (icon_imagename) VALUES (:iconImageName);';
$pdo->prepare($sql_add)->execute(['iconImageName'=>basename($_FILES["uploadNewImage"]["name"])]);

I got it working now, and am not able to delete this topic.

Do you know the value of that variable?

Sponsor our Newsletter | Privacy Policy | Terms of Service