1

I have an add form, where users can add an image, once they click submit I want the image to be saved into a certain folder located called Event_images (which I use to display the image on another page where I display the image along with other details they inputted. But once I submit the form, the image goes to the MYSQL database, but isn't in the folder where I direct it to. My code and a screenshot are provided. I want the image to be displayed in the Event_images folder I have created. Thank you in advance.

 <?php 

  $event_img = $_FILES['event_img']['name'];

$tempimage = $_FILES['event_img']['tempname'];
move_uploaded_file($tempimage,"Event_images/$event_img");

 ?>

The screenshot I have provided is what happens when I try to display the information (because nothing is in the folder which I direct to)

chris85
  • 23,846
  • 7
  • 34
  • 51

2 Answers2

0

['tempname'] isn't an index of $_FILES. See here.

Use $tempimage = $_FILES['event_img']['tmp_name']; instead. I tested locally and it worked fine. If you're using PHP 7 you may need to use { } around $event_img when you move the file so it'll process that variable.

  • 1
    would this be the correct syntax? – Naeem Dogar Apr 10 '18 at 13:58
  • @NaeemDogar Bingo! – Brian Pluhar Apr 10 '18 at 14:00
  • Thank you but still nothing is happening, could it be my path? The Event_images folder is located in htdocs/Admin_area/Event_images, but even when I set this as the path it doesn't save? – Naeem Dogar Apr 10 '18 at 14:06
  • Did it generate an `error_log` on your server? You might not have proper permissions to move the file (Just dealt with this exact issue with moving an image the other day). – Brian Pluhar Apr 10 '18 at 14:18
  • It came up with no error, simply just refreshed the webpage as expected, but with no image being saved, do you know how I could gain permission or check if I have permission? Thank you:) – Naeem Dogar Apr 10 '18 at 14:22
  • @NaeemDogar `images` and `tmp_file_upload` are only writable by the root user, and the script you're executing is being processed by httpd, check out [this answer here](https://stackoverflow.com/a/8104498/8411905). – Brian Pluhar Apr 10 '18 at 14:25
0

Does this work?

$event_img = rand(1000,100000)."-".$_FILES['event_img']['name']; 
   $tempimage = $_FILES['event_img']['temp_name']; 
    $folder="Event_images/";
      move_uploaded_file($file_loc,$folder.$event_img);
Glen
  • 39
  • 1
  • 2
  • 14