User uploaded image

This information would of been nice 2 hours ago.

You need to specify the full absolute path to that folder.

I did this…

sudo chmod -R 0777 /var/www/part/img
[Sun Mar 11 23:56:45.732334 2018] [:error] [pid 7769] [client ::1:36768] PHP Warning:  move_uploaded_file(/img/user_upl/home_icon_android.png): failed to open stream: No such file or directory in /var/www/part/upload.php on line 39, referer: http://localhost/part/test.html
[Sun Mar 11 23:56:45.732380 2018] [:error] [pid 7769] [client ::1:36768] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpyyjJ4A' to '/img/user_upl/home_icon_android.png' in /var/www/part/upload.php on line 39, referer: http://localhost/part/test.html


Changing this line

//$target_dir = "/img/user_upl/";

to

$target_dir = "img/user_upl/";

Seems to resolve it.

There are so much problems with your script by the way.

  • You’ll get an undefined index error if you try accessing the file directly.
  • You’re checking the type of the file extension and not the actual file.
  • You’re using the base name of the file which is obviously not safe.
  • You’re using if(isset($_POST['submit'])) which should not be relied on.

To fix these problems, I suggest

  • Wrapping everything in a single $_SERVER['REQUEST_METHOD'] submission checking.
  • Check to make sure the tmp file is an image instead of the type of the file extension since $_FILES[...]['type'] actually refers back to the mime type of the file extension. Though apparently you are already doing this. So you don’t need $_FILES[...]['type'] .
  • Use your own file naming conventions so that people don’t use theirs. This can be an issue if the file already exists. You’re basically telling the user to “rename” their own photos which actually is not a good thing. What if they are uploading from a phone? Most phones usually use the naming convention IMG_xxxx.JPG or IMG_xxxx.jpg.
  • Use $_SERVER['REQUEST_METHOD'] instead of the amateur hack.

That was example I tried to learn from w3 school…:slight_smile:

w3schools is not a good place you want to be learning PHP code from. I have already done an extensive discussion on uploading systems and how to implement proper security. You can find them if you dig deep enough on this forum.

Last question:)

How would I place some parts of the code into different file yet be able to share variables as if they were in the same file?

I’d say use require. People abuse the include function too much not understanding what it really does.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.