/    Sign up×
Community /Pin to ProfileBookmark

Why My If File Exists Condition Fails ?

Php Experts,

For some reason I can’t get the “if(file_exists” to work. I don’t want the user uploading the same file again. If he tries then should get error alert: “Error: You have already uploaded a video file to verify your ID!”

On the comments, I have written in CAPITALS such as: IS THIS LINE CORRECT ? IS THIS LINE OK ? IS LINE OK ? CORRECT ?

I need your attention most on those particular lines to tell me if I wrote those lines correct or not.

Those are the places where I need your attention the most to tell me if I made any mistakes on those lines or not and if so then what the mistakes are.

If you spot any other errors then kindly let me know. I’d appreciate sample snippets (as your corrections to my mistakes on the mistaken code lines) to what you think I should substitute my lines to.

Thank You!

Here is my attempt:

[code]
<?php

//Required PHP Files.
include ‘config.php’;
include ‘header.php’;
include ‘account_header.php’;

if (!$conn)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print “$errno: $errorn”;
exit();
}

if($_SERVER[“REQUEST_METHOD”] == “POST”)
{
//Check whether the file was uploaded or not without any errors.
if(!isset($_FILES[“id_verification_video_file”]) && $_FILES[“id_verification_video_file”][“Error”] == 0)
{
$Errors = Array();
$Errors[] = “Error: ” . $_FILES[“id_verification_video_file”] [“ERROR”];
print_r($_FILES); ?><br><?php
print_r($_ERRORS);
exit();
}
else
{
//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)) //IS THIS LINE CORRECT ?
{
$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”)) //IS THIS LINE CORRECT ?
if(file_exists($directory_path . $user . ‘/’ . $file_name)) //RETYPE
{
$Errors[] = “Error: You have already uploaded a video file to verify your ID!”;
exit();
}
else
{
//Feed allowed File Extensions List.
$allowed_file_extensions = array(“mp4” => “video/mp4”);

//Feed allowed File Size.
$max_file_size_allowed_in_bytes = 1024*1024*100; //Allowed limit: 100MB.
$max_file_size_allowed_in_kilobytes = 1024*100;
$max_file_size_allowed_in_megabytes = 100;

$max_file_size_allowed = “$max_file_size_allowed_in_bytes”;

//Verify File Extension.
if(!array_key_exists($file_extension, $allowed_file_extensions)) die(“Error: Select a valid video file format. Select an Mp4 file.”);
//Verify MIME Type of the File.
elseif(!in_array($file_type, $allowed_file_extensions))
{
$Errors[] = “Error: There was a problem uploading your file $file_name! Make sure your file is an MP4 video file. You may try again.”; //IS THIS LINE CORRECT ?
}
//Verify File Size. Allowed Max Limit: 100MB.
elseif($file_size>$max_file_size_allowed) die(“Error: Your Video File Size is larger than the allowed limit of: $max_file_size_allowed_in_megabytes.”);
//Move uploaded File to newly created directory on the server.
move_uploaded_file(“$file_tmp”, “$directory_path” . “$user/” . “$file_name”); //IS THIS LINE CORRECT ?
//Notify user their Id Verification Video File was uploaded successfully.
echo “Your Video File “$file_name” has been uploaded successfully!”;
exit();
}
}
}
?>

<form METHOD=”POST” ACTION=”” enctype=”multipart/form-data”>
<fieldset>
<p align=”left”><h3><?php $site_name ?> ID Video Verification Form</h3></p>
<div class=”form-group”>
<p align=”left”<label>Video File: </label>
<input type=”file” name=”id_verification_video_file” id=”id_verification_video_file” value=”uploaded ‘Id Verification Video File.'”></p>
</div>
</fieldset>
<p align=”left”><button type=”submit” class=”btn btn-default” name=”id_verification_video_file_submit”>Submit!</button></p>
</form>

</body>
</html>
[/code]

Error reporting is in one of the included files. Like so:

[code]
<?php

//ERROR REPORTING CODES.
declare(strict_types=1);
ini_set(‘display_errors’, ‘1’);
ini_set(‘display_startup_errors’, ‘1’);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

?>
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@site-developerauthorSep 30.2018 — My if(file exists) function still not working.

Any ideas how to fix this ?

Neither these 2 in lines 48 & 49 are working:

<i>
</i>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.


<i>
</i>&lt;?php

//Required PHP Files.
include 'config.php';
include 'header.php';
include 'account_header.php';

if (!$conn)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print "$errno: $errorn";
exit();
}

if($_SERVER["REQUEST_METHOD"] == "POST")
{
//Check whether the file was uploaded or not without any errors.
if(!isset($_FILES["id_verification_video_file"]) &amp;&amp; $_FILES["id_verification_video_file"]["Error"] == 0)
{
$Errors = Array();
$Errors[] = "Error: " . $_FILES["id_verification_video_file"] ["ERROR"];
print_r($_FILES); ?&gt;&lt;br&gt;&lt;?php
print_r($_ERRORS);
exit();
}
else
{
//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)) //IS THIS LINE CORRECT ?
{
$mode = "0777";
mkdir($directory_path . $user, "$mode", TRUE); //IS THIS LINE CORRECT ?
}

<i> </i> //Grab Uploading File details.
<i> </i> $Errors = Array(); //SHOULD I KEEP THIS LINE OR NOT ?
<i> </i> $file_name = $_FILES["id_verification_video_file"]["name"];
<i> </i> $file_tmp = $_FILES["id_verification_video_file"]["tmp_name"];
<i> </i> $file_type = $_FILES["id_verification_video_file"]["type"];
<i> </i> $file_size = $_FILES["id_verification_video_file"]["size"];
<i> </i> $file_error = $_FILES['id_verification_video_file']['error'];
<i> </i>
<i> </i> //Grab Uploading File Extension details.
<i> </i> $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
<i> </i> 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!"
<i> </i> //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.
<i> </i> {
<i> </i> $Errors[] = "Error: You have already uploaded a video file to verify your ID!";
<i> </i> exit();
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> //Feed allowed File Extensions List.
<i> </i> $allowed_file_extensions = array("mp4" =&gt; "video/mp4");
<i> </i>
<i> </i> //Feed allowed File Size.
<i> </i> $max_file_size_allowed_in_bytes = 1024*1024*100; //Allowed limit: 100MB.
<i> </i> $max_file_size_allowed_in_kilobytes = 1024*100;
<i> </i> $max_file_size_allowed_in_megabytes = 100;
<i> </i>
<i> </i> $max_file_size_allowed = "$max_file_size_allowed_in_bytes";
<i> </i>
<i> </i> //Verify File Extension.
<i> </i> if(!array_key_exists($file_extension, $allowed_file_extensions)) die("Error: Select a valid video file format. Select an Mp4 file.");
<i> </i> //Verify MIME Type of the File.
<i> </i> elseif(!in_array($file_type, $allowed_file_extensions))
<i> </i> {
<i> </i> $Errors[] = "Error: There was a problem uploading your file $file_name! Make sure your file is an MP4 video file. You may try again."; //IS THIS LINE CORRECT ?
<i> </i> }
<i> </i> //Verify File Size. Allowed Max Limit: 100MB.
<i> </i> elseif($file_size&gt;$max_file_size_allowed) die("Error: Your Video File Size is larger than the allowed limit of: $max_file_size_allowed_in_megabytes.");
<i> </i> //Move uploaded File to newly created directory on the server.
<i> </i> move_uploaded_file("$file_tmp", "$directory_path" . "$user/" . "$file_name"); //IS THIS LINE CORRECT ?
<i> </i> //Notify user their Id Verification Video File was uploaded successfully.
<i> </i> echo "Your Video File "$file_name" has been uploaded successfully!";
<i> </i> exit();
<i> </i> }
<i> </i> }
<i> </i>}
?&gt;

&lt;form METHOD="POST" ACTION="" enctype="multipart/form-data"&gt;
&lt;fieldset&gt;
&lt;p align="left"&gt;&lt;h3&gt;&lt;?php $site_name ?&gt; ID Video Verification Form&lt;/h3&gt;&lt;/p&gt;
&lt;div class="form-group"&gt;
&lt;p align="left"&lt;label&gt;Video File: &lt;/label&gt;
&lt;input type="file" name="id_verification_video_file" id="id_verification_video_file" value="uploaded 'Id Verification Video File.'"&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/fieldset&gt;
&lt;p align="left"&gt;&lt;button type="submit" class="btn btn-default" name="id_verification_video_file_submit"&gt;Submit!&lt;/button&gt;&lt;/p&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@rootSep 30.2018 — if( file_exists( "{$directory_path}{$user}/{$file_name}" ) ) ...
or
if( file_exists( $directory_path . $user . "/" . $file_name ) ) ...
You can wrap quotes around variables but remember that a " " in a string is a space in the test...

So if you are expecting a string of /users/some/directoryUSERACC/file.ext what you are getting (based on your example ) is something like this /users/some/directory . USERACC/ . file.ext and not the desired path because of the way you wrote the string out.

If you are going to be repeatedly assessing that type of string, then I suggest that you use sprintf() to spit out a formatted path name like so... $testPath = sprintf("%s%s/%s", $directory_path, $user, $file_name ); and then $testPath will contain the formatted string that you then use in place of the string in the if( file_exists( $testPath ) ) ... type of thing.

Try that and see if it sorts the strings out for you.
×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...