Asumi Posted December 8, 2014 Share Posted December 8, 2014 (edited) Heya all, as the title says, i meet some issues when uploading files to my server, here is the code; $anime = $_POST['anime']; $anime = $file = str_replace(' ', '\ ', $anime); $anime = "Sous-titres/".$anime."/"; $target_dir = $anime; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; if(isset($_POST["submit"])) { // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 50000000) { echo "Sorry, your file is too large."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded."; } else { var_dump ($target_dir); echo "Sorry, there was an error uploading your file."; } } } So, i'm attempting to acces to a directory taking the name of the anime that the admin put in the input type="text", for upload the file selected in. Error with the var_dump: string(28) "Sous-titres/Akame\ ga\ kill/" Sorry, there was an error uploading your file. Hope you can help me ! Edited December 8, 2014 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/292959-some-problems-with-upload-directory/ Share on other sites More sharing options...
WinstonLA Posted December 8, 2014 Share Posted December 8, 2014 (edited) Maybe the directory that you're trying upload file in, hasn't write permissions. Set error reporting level to error_reporting(-1); May some errors will be output Edited December 8, 2014 by WinstonLA Quote Link to comment https://forums.phpfreaks.com/topic/292959-some-problems-with-upload-directory/#findComment-1498963 Share on other sites More sharing options...
Asumi Posted December 8, 2014 Author Share Posted December 8, 2014 (edited) Hey WinstonLA, thank's for the answer, i had a problem like that at the beginning and corrected it, like the permissions are; drwxrwxrwx www-data www-data The problem is, if i replace this line : $anime = "Sous-titres/".$anime."/"; With this: $anime = "Sous-titres/$anime"; My file is uploaded, but on the directory "Sous-titres", and the filename is something like "Akame\ ga\ killFilename.jpg", don't know what is wrong The directory have the same permissions than Sous-titres. Activating the error_reporting; Warning: move_uploaded_file(Sous-titres/Akame\ ga\ kill/Tulips.jpg): failed to open stream: No such file or directory in /var/www/subs/index.php on line 527 Warning: move_uploaded_file(): Unable to move '/tmp/php4k6709' to 'Sous-titres/Akame\ ga\ kill/Tulips.jpg' in /var/www/subs/index.php on line 527 Edited December 8, 2014 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/292959-some-problems-with-upload-directory/#findComment-1498964 Share on other sites More sharing options...
WinstonLA Posted December 8, 2014 Share Posted December 8, 2014 (edited) As I know filename can't be Akame\ ga\ killFilename.jpg because Akame\ ga may be considered as a directory. You should avoid slashes in filenames or create appropriate directories and then specify slashes in filename Edited December 8, 2014 by WinstonLA Quote Link to comment https://forums.phpfreaks.com/topic/292959-some-problems-with-upload-directory/#findComment-1498966 Share on other sites More sharing options...
Asumi Posted December 8, 2014 Author Share Posted December 8, 2014 (edited) The problem is the spaces in the directory "Akame ga kill", in debian, for cd to that directory i need to do cd Akame\ Ga\ Kill. So i thinked that i needed to add the slashes with the str_replace here ; $anime = $file = str_replace(' ', '\ ', $anime); I can't really don't use spaces here, because the directory is created with the Anime name, when the Admin create a new one, and most of the animes have a composed name. (Well, i can tell to admins to use CamelCase for example, but i'm sure that we can deal with this problem !) Edited December 8, 2014 by Asumi Quote Link to comment https://forums.phpfreaks.com/topic/292959-some-problems-with-upload-directory/#findComment-1498967 Share on other sites More sharing options...
WinstonLA Posted December 8, 2014 Share Posted December 8, 2014 OK. Try comment this line $anime = $file = str_replace(' ', '\ ', $anime); And after line $anime = "Sous-titres/".$anime."/"; write var_dump(file_exists($anime)); this will show that is there that directory. If so than uploading should be successful, if no, you need find that path that is will be returned true by file_exists() function. Quote Link to comment https://forums.phpfreaks.com/topic/292959-some-problems-with-upload-directory/#findComment-1498969 Share on other sites More sharing options...
Ch0cu3r Posted December 8, 2014 Share Posted December 8, 2014 So i thinked that i needed to add the slashes with the str_replace here ; $anime = $file = str_replace(' ', '\ ', $anime); You dont need to escape the spaces in the file path. PHP will treat the \ as a directory separator. Quote Link to comment https://forums.phpfreaks.com/topic/292959-some-problems-with-upload-directory/#findComment-1498970 Share on other sites More sharing options...
Asumi Posted December 8, 2014 Author Share Posted December 8, 2014 (edited) Well, i did what you say, and had this; bool(true) string(26) "Sous-titres/Akame ga kill/" Sorry, there was an error uploading your file. So if i understand right, the directory exists , but the upload can't be done for some reason ? Ok Ch0cu3r ! Good to know Edited December 8, 2014 by Asumi Quote Link to comment https://forums.phpfreaks.com/topic/292959-some-problems-with-upload-directory/#findComment-1498971 Share on other sites More sharing options...
Asumi Posted December 8, 2014 Author Share Posted December 8, 2014 (edited) Well, was testing some possibilities and i read again this line ; Maybe the directory that you're trying upload file in, hasn't write permissions. Set error reporting level to error_reporting(-1); May some errors will be output And I told me, well, gonna try to change the permissions via ssh on these directories. So i did; chown -R www-data:www-data Sous-titres and; chmod -R 777 Sous-titres And now, it's work ! Thank's very much WinstonLA ! You are a god PS: I changed thise line on folder creation; $oldumask = umask(002); Edited December 8, 2014 by Asumi Quote Link to comment https://forums.phpfreaks.com/topic/292959-some-problems-with-upload-directory/#findComment-1498977 Share on other sites More sharing options...
WinstonLA Posted December 8, 2014 Share Posted December 8, 2014 @Asumi You're welcome Please mark this topic as answered if you can do it Quote Link to comment https://forums.phpfreaks.com/topic/292959-some-problems-with-upload-directory/#findComment-1499001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.