-1

I have a form where i have an input type as below:

topic image: <input type="file" name="pic" accept="image/*">
<input type="submit" name="submit" value="submit">

After that, i saved what i got in a variable:

$image=$_POST['pic'];

Then, i used the move_uploaded_file function but it is not working, here is the code:

if(isset($_POST['submit']))
{
   move_uploaded_file($image,'images/'.$image);
}

So, i want the file to be saved at a folder i want called "images" but it doesn't work. Could someone help me and explain me why?

JStyle
  • 9
  • 1
  • 4
  • 1
    You need to use `$_FILES` when working with file-uploads. Please see: http://php.net/manual/en/features.file-upload.post-method.php – M. Eriksson Jan 07 '17 at 20:22
  • 1
    1. `$_POST` is not the correct superglobal to access/manipulate file data, use `$_FILES`. 2. Make sure that you have `enctype= multipart/form-data` in your form. 3. `move_uploaded_file()` function call is wrong. RTM, [http://php.net/manual/en/function.move-uploaded-file.php](http://php.net/manual/en/function.move-uploaded-file.php) – Rajdeep Paul Jan 07 '17 at 20:22

1 Answers1

1

You must learn the code basics. You should use the $_FILES super global for uploading files to your server. It is an associative array of uploaded file items and some of their properties.

Also you must add enctype="multipart/form-data" to your HTML form.

EXAMPLE:

<form action="" method="post" enctype="multipart/form-data">
topic image: <input type="file" name="pic" accept="image/*">
<input type="submit" name="submit" value="submit">
</form>
<?php

if(isset($_POST['submit']))
{
   //This is not a good file upload code sample. You have to improve it.
   $image=$_FILES["pic"]["tmp_name"];
   $imageName = $_FILES["pic"]["name"]
   move_uploaded_file($image,'images/'.$imageName );
}
?>
Martin
  • 22,212
  • 11
  • 70
  • 132
Hakan SONMEZ
  • 2,176
  • 2
  • 21
  • 32