/    Sign up×
Community /Pin to ProfileBookmark

Why “is_dir” Function Fails ?

Php Masters,

Below is my code to upload a video file to my mysql tbl.
If user tries uploading wrong type of file then he should get error.
And if he tries uploading a file beyond the allowed limit then too he should get error.
Do you think my code is ok ?

One thing though, I am trying to get the script to upload the user’s video file on this path:
uploads/videos/id_verifications/$user
And so, if the directory does not exist then the script should create it. Else just upload the file to the directory without creating/recreating it.
My code looks like this:

[code]
//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);
}
[/code]

Problem is, if the directory already exists then I get error which I should not because if the directory exists then the script should just upload it to that directory without creating/recreating it (based on the condition).
Error:
Warning: mkdir(): File exists in C:xampphtdocstextupload_v1.php on line 35

Line 35 is this:

[code]
mkdir(“$directory_path” . “$user”, $mode, TRUE);
[/code]

What is wrong ? Why I get this error ? Not supposed to as the condition states that if the directory does not exist then create it. Else just upload the file to it without creating/recreating it.

Full Code:

[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”))
{
$mode = “0777”;
mkdir(“$directory_path” . “$user”, $mode, TRUE);
}
else
{

}

//Grab uploading File details.
$Errors = Array();
$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”];

//Grab Uploading File Extension details.
$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);

if(file_exists(“$directory_path” . “$user/” . “$file_name”))
{
$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.”;
}
//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”);
//Notify user their Id Verification Video File was uploaded successfully.
echo “Your Video File “$file_name” has been uploaded successfully!”;
exit();
}
}
}
?>

<form enctype=”multipart/form-data” ACTION=”” METHOD=”POST”>
<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 and it goes like this:

[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]

Beats me! Can you figure this thing out ? Any ideas ?

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 04.2018 — How about the ' . "user" ' part?
Copy linkTweet thisAlerts:
@site-developerauthorSep 30.2018 — @ginerjm#1594572

So, how do you reckon it should be then ?

Here is my latest, can you spot any errors ?

Note my comments.

<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")) //IS THIS LINE CORRECT ?
<i> </i> if(file_exists($directory_path . $user . '/' . $file_name)) //RETYPE
<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 — Echo the string path you are evaluating, is it actually a DIR path or a File path? is_dir() will only return true if it is a directory not a file path.

http://php.net/manual/en/function.is-dir.php

Like anything that give an error,.. Echo it out to the browser to see what it is that is actually being evaluated.
Copy linkTweet thisAlerts:
@NogDogOct 01.2018 — Umm...where is $user getting set?
Copy linkTweet thisAlerts:
@rootOct 01.2018 — Also I suggest that you double check your code for typo's where you have spaces where spaces shouldn't be...

$_FILES["id_verification_video_file"] ["ERROR"] to $_FILES["id_verification_video_file"]["ERROR"]

Also web forms use <input and the submit button is not a <button but an &lt;input name="submit" type="submit" value="Submit"&gt; and please use web forms properly if you want them to function correctly.

eg. &lt;form name="myForm" action="" method="post" enctype="multipart/form-data"&gt;
Copy linkTweet thisAlerts:
@site-developerauthorOct 02.2018 — @NogDog#1596366

In one of the included files.
Copy linkTweet thisAlerts:
@ginerjmOct 03.2018 — This post just seems to go on and on.

You posted this line:
<i>
</i> if(!is_dir($directory_path . $user)) //IS THIS LINE CORRECT ?

and ask with your comment if is correct. Correct how? Syntactically? Why, Yes It Is. But perhaps you could help yourself out of what seems to be a problem by simply doing some DEBUGGING yourself and echo'ing out the vars you are using to see what they REALLY CONTAIN not what you are assuming they contain.

Try this:
<i>
</i>echo "checking if folder : '" . $directory_path . $user ."' exists";
exit();

Show yourself what you are working with to be sure your script is doing it's thing correctly and stop right there. If it is working you can move those two lines further down and do some more DEBUGGING.

That's how this game works.
Copy linkTweet thisAlerts:
@site-developerauthorOct 13.2018 — @root#1596367

You mean, these are ok ?
<i>
</i>&lt;p align="left"&gt;&lt;input type="submit" class="btn btn-default" name="submit" value = "submit"&gt;Register!&lt;/button&gt;&lt;/p&gt;


<i>
</i>&lt;p align="left"&gt;&lt;input type="submit" class="btn btn-default" name="submit" value = "submit"&gt;Login!&lt;/button&gt;&lt;/p&gt;


<i>
</i>&lt;p align="left"&gt;&lt;input type="submit" class="btn btn-default" name="submit" value = "submit"&gt;Submit!&lt;/button&gt;&lt;/p&gt;
Copy linkTweet thisAlerts:
@ginerjmOct 13.2018 — Do you even have a resources that you read and follow in an attempt to learn?????

You are writing an input statement but then you follow it with a end-button tag. Does that make sense to you?

1 - one doesn't mix tags so this is patently wrong.

2 - the input tag doesn't require any end tag so once again you are mistaken here.

3 - a little formatting in your code writing might make things look better in the end. Try this:
<i>
</i>&lt;input type="submit" class="btn btn-default" name="btn" value = "Submit"&gt;

The caps on the value will actually show up on your web page with caps. Make sense?

PS - I always (just about always) use the name of 'btn' for my submit buttons. That way in my code when I'm am looking to see what button the user clicked on I can always retrieve it. I use forms that often have multiple buttons in them so this standard makes it easy to work with them.
Copy linkTweet thisAlerts:
@rootOct 13.2018 — There is a process to learning HTML and it is like this.

Learn HTML

Learn CSS styling of HTML and its DOM elements

Learn JavaScript and how to read, manipulate DOM

Learn how to make simple PHP HTML and JavaScript pages

Learn to add a database to the mix

Learn to use JavaScript AJAX with Database

Learn to use lazyboyz tools like JQuery

There are ways of doing things that will improve code and also REDUCE coding to its absolute minimum, this is where PHP and databases can come in handy, by only putting in what is needed on a page, you make it ultra fast.

If you ever wonder why some pages take ages to load, well take a peak at the stuff under the hood, that is considered good programming and tools used by the industry, an abomination is what I call it, no skill or finesse, just a one size fits all, like it or lump it misses.

So once you have learnt a few useful tips on forms, how JavaScript has been replaced with HTML5 inputs and how you can use the technologies together, you would stop running in to these problems.
Copy linkTweet thisAlerts:
@ginerjmOct 13.2018 — That's a pretty long list of things to learn. Considering how often the OP want help from us to show him how things are done, we are setting ourselves up for a major project here!! ?
Copy linkTweet thisAlerts:
@rootOct 13.2018 — Yep... maybe but there are tutorials out there that teach the proper use of HTML and JavaScript


I learned JavaScript, they first taught HTML and the layout, CSS then JavaScript on week 3 ... and by week 4, you were expected to complete a simple login form using HTML and JavaScript only, bad practice by todays standards but it helped to understand validation processing a form and its fields easily...

Its like learning anything, there is a process involved and it often starts with nothing to do with what the person wants but it is what they need.

×

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 4.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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...