user1174762
user1174762

Reputation: 593

move uploaded file in PHP

I want to allow a user the upload an image (file) for their profile picture, on my website. They upload the image via an HTML form, but I am having trouble moving the file to the folder I want it to. I don't want to mess with the php.ini file to change the upload path. I want to use move_uploaded_file().

Here is my HTML:

<form enctype="multipart/form-data" method="post" action="upload_img.php">
    <input type="hidden" name="MAX_FILE_SIZE" value="32768"/>
    <input type="text" name="name" value=""/>
    <input type="file" name="picture" value="picture"/>
    <input type="submit" name="submit" value="upload"/>
</form>

And my PHP:

<?php
  define('GW_UPLOADPATH', 'images/');
  $picture= $_FILES['picture']['name'];
  $name= $_POST['name'];
  $tmp= $_FILES['picture']['tmp_name'];

  var_dump($picture);
  var_dump($name);
  var_dump($tmp);

  $connect= mysqli_connect(//connect params)
  or die('error connecting with the database');

  $query= "INSERT INTO pics (pic, name) VALUES ('$picture', '$name')";

  $target= GW_UPLOADPATH . $picture ;

  if (move_uploaded_file($_FILES['picture']['tmp_name'], $target);))
     {
      mysqli_query($connect, $query)
          or die('error with query');
     }

?>

I know the file gets uploaded to the tmp folder because I can see it, but it is named sess_96bsj29ub3tndnd2853d24k38adrbqoo.file Is that what should happen? What am I doing wrong?

Upvotes: 0

Views: 4721

Answers (2)

Mostafa Maklad
Mostafa Maklad

Reputation: 347

Just remove semicolon form if statement like:

if (move_uploaded_file($_FILES['picture']['tmp_name'], $target)))
    {

    mysqli_query($connect, $query)
    or die('error with query');
    }

Upvotes: 1

Taha Paksu
Taha Paksu

Reputation: 15616

$_FILES['picture']['tmp_name'] 

will point the file in that folder and when you call this line

move_uploaded_file($_FILES['picture']['tmp_name'], $target);

you'd get the file in your temp folder to wherever you want. just set $target to be a valid and existing directory.

Upvotes: 3

Related Questions