ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

if(file_exists..) is Failing!

(1/1)

The Code Queryer:
Php Whizes!

A certain video file exists in a certain directory on my hdd. I get the script to upload the same file again expecting to see error message (based on my coding) that the file has already been uploaded. But, I see no error appearing. Why is that ? Trouble in this line:

--- ---if(file_exists("$directory_path . $user/ . $file_name")) //THIS LINE IS NOT GIVING THE ERROR THAT FILE HAS ALREADY BEEN UPLOADED. INSTEAD GIVES THE ECHO THAT IS 26 LINES BELOW HERE: "Your Video File \"$file_name\" has been uploaded successfully!"

Context of the script:

--- ---
//Feed Id Verification Video File Upload Directory path.
$directory_path = "uploads/videos/id_verifications/";
//Make Directory under $user in 'uploads/videos/id_verifications' Folder.
if(!is_dir($directory_path . $user))
{
$mode = "0777";
mkdir($directory_path . $user, "$mode", TRUE); //IS THIS LINE CORRECT ?
}

//Grab Uploading File details.
$Errors = Array(); //SHOULD I KEEP THIS LINE OR NOT ?
$file_name = $_FILES["id_verification_video_file"]["name"];
$file_tmp = $_FILES["id_verification_video_file"]["tmp_name"];
$file_type = $_FILES["id_verification_video_file"]["type"];
$file_size = $_FILES["id_verification_video_file"]["size"];
$file_error = $_FILES['id_verification_video_file']['error'];

//Grab Uploading File Extension details.
$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
if(file_exists("$directory_path . $user/ . $file_name")) //THIS LINE IS NOT GIVING THE ERROR THAT FILE HAS ALREADY BEEN UPLOADED. INSTEAD GIVES THE ECHO THAT IS 26 LINES BELOW HERE: "Your Video File \"$file_name\" has been uploaded successfully!"
//if(file_exists($directory_path . $user . '/' . $file_name)) //THIS LINE IS NOT GIVING THE ERROR THAT FILE HAS ALREADY BEEN UPLOADED. INSTEAD SHOWING BLANK WHITE PAGE.
{
$Errors[] = "Error: You have already uploaded a video file to verify your ID!";
exit();
    }

I need help where you see me asking you a question on my comments (on my above code).

mouser:
In your code, the commented out version of the line is closer to what you want.  By putting double quotes around everything you are not getting the string you want.

Here's a tip for you, use some intermediate variables.  Instead of going right to
--- Code: PHP ---if(file_exists($directory_path . $user . '/' . $file_name))
Try instead using a few lines like

--- Code: PHP ---$fpath = $directory_path . $user . '/' . $file_name;echo 'About to check if this file exists: ' . $fpath;if (file_exists($fpath))
Then maybe you will see the problem with how you are creating the full file path.  You might find that you've got an extra / where you dont expect it, or that $user is not what you expect, etc.

The Code Queryer:
In your code, the commented out version of the line is closer to what you want.  By putting double quotes around everything you are not getting the string you want.

Here's a tip for you, use some intermediate variables.  Instead of going right to
--- Code: PHP ---if(file_exists($directory_path . $user . '/' . $file_name))
Try instead using a few lines like

--- Code: PHP ---$fpath = $directory_path . $user . '/' . $file_name;echo 'About to check if this file exists: ' . $fpath;if (file_exists($fpath))
Then maybe you will see the problem with how you are creating the full file path.  You might find that you've got an extra / where you dont expect it, or that $user is not what you expect, etc.
-mouser (April 06, 2019, 03:31 PM)
--- End quote ---

Thanks. All this time was experimentin on my localhost xampp. Now tried my website and get this error:
"Warning: mkdir(): Permission denied in /home/domain/public_html/upload.php on line 35".

Shades:
That is most likely a Linux server that hosts your website. As a backup for my locally running web server, I have a another domain hosted in a different continent. But access to that site is not managed by cPanel. In that web GUI interface it is pretty easy to alter access rights for files/folders. But with cPanel I don't know if you have the same functionality. If not, you could try to setup a Putty session to your web-server and use:

    CHMOD /R 775 /the.exact.folder.path.on.your.hosted.server.which.is.case.sensitive    (on a shared host server it can be a very different path than you expect from the onset) 

- CHMOD   name of the command to alter access rights of files and/or folders on Linux.
- /R           recursive. All sub folders below the folder you want to alter are altered as well.
- 775         That code indicates the root account has complete access.
                 The user groups you assign to this folder have complete access as well (the user group linked to the web server software is one of these groups).
                 All other users can read and execute files in the folder you select. This is a good value during development, but not if you serve your website to the public.

Afterwards you should change from 775 to 755 and check if your website still works as you intended. It should work and if it doesn't, revise your code until it does.

Be very careful with this line, it is very easy to open up your website too much and that is something you really don't want to do.

Navigation

[0] Message Index

Go to full version