Could not upload file via php

I am trying to upload a file and then save it by changing its name. However, this does not work and gives me this error.

Warning: move_uploaded_file (uploads / 564b68ef0e2f8 | 3d-pc-nature-wallpaper.jpg): stream could not be opened: invalid argument in C: \ wamp \ www \ f \ ajax.php on line 157

Warning: move_uploaded_file (): Unable to move 'C: \ wamp \ tmp \ phpA364.tmp' to 'uploads / 564b68ef0e2f8 | 3d-pc-nature-wallpaper.jpg 'in C: \ wamp \ www \ f \ ajax.php on line 157

I checked and the file will go into $ _ FILES correctly. Here is my code.

$rand_img = uniqid(); $file_upload_folder = "uploads"; $finalImgLink = $file_upload_folder . '/' . $rand_img . '|' . $_FILES['file']['name']; //move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/$rand_img|' . $_FILES['file']['name']); if(move_uploaded_file($_FILES['file']['tmp_name'], $finalImgLink)) { echo "ok"; } else { echo "not ok"; } 

What is wrong here?

+5
source share
2 answers

You cannot use pipe (|) in file names. This is the most likely cause of the error you are experiencing. Try changing it to an underscore or dash.

 $rand_img = uniqid(); $file_upload_folder = "uploads"; $finalImgLink = $file_upload_folder . '/' . $rand_img . '_' . $_FILES['file']['name']; //move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/$rand_img|' . $_FILES['file']['name']); if(move_uploaded_file($_FILES['file']['tmp_name'], $finalImgLink)) { echo "ok"; } else { echo "not ok"; } 

In windows, the file name cannot contain any of the following characters:

\ /: *? "<> |

+4
source

You can not use | in your file name. If you want a delimiter, you can have something like,. or any specific line, for example abc , xy , etc.,

So, update your code as follows.

 $finalImgLink = $file_upload_folder . '/' . $rand_img . ".." . $_FILES['file']['name']; 
+1
source

Source: https://habr.com/ru/post/1236172/


All Articles