topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday April 16, 2024, 5:22 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: if(file_exists..) is Failing!  (Read 4540 times)

The Code Queryer

  • Participant
  • Joined in 2019
  • *
  • default avatar
  • Posts: 33
    • View Profile
    • Donate to Member
if(file_exists..) is Failing!
« on: April 06, 2019, 12:47 PM »
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

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,900
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: if(file_exists..) is Failing!
« Reply #1 on: April 06, 2019, 03:31 PM »
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 [Select]
  1. if(file_exists($directory_path . $user . '/' . $file_name))

Try instead using a few lines like
Code: PHP [Select]
  1. $fpath = $directory_path . $user . '/' . $file_name;
  2. echo 'About to check if this file exists: ' . $fpath;
  3. 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

  • Participant
  • Joined in 2019
  • *
  • default avatar
  • Posts: 33
    • View Profile
    • Donate to Member
Re: if(file_exists..) is Failing!
« Reply #2 on: April 08, 2019, 01:45 PM »
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 [Select]
  1. if(file_exists($directory_path . $user . '/' . $file_name))

Try instead using a few lines like
Code: PHP [Select]
  1. $fpath = $directory_path . $user . '/' . $file_name;
  2. echo 'About to check if this file exists: ' . $fpath;
  3. 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.

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

  • Member
  • Joined in 2006
  • **
  • Posts: 2,922
    • View Profile
    • Donate to Member
Re: if(file_exists..) is Failing!
« Reply #3 on: April 08, 2019, 10:24 PM »
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.