Jump to content

Some problems with upload directory.


Asumi

Recommended Posts

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 by Zane
Link to comment
Share on other sites

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 by Zane
Link to comment
Share on other sites

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 by WinstonLA
Link to comment
Share on other sites

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 :P !)

Edited by Asumi
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Asumi
Link to comment
Share on other sites

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 :D

 

PS: I changed thise line on folder creation; $oldumask = umask(002);

Edited by Asumi
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.