Jump to content

check if a file is exist in folder


sigmahokies

Recommended Posts

Hi everyone, I'm still learning, but getting intermediate in PHP now, but it is still challenge to learn. I'm trying to have php check to see if one file inside folder in server, seem I could not get it right, but I tested it on other site, it works, but not this script, I don't understand why it won't work...maybe logical is wrong?

here my code:

	if ($_POST['video']) {
	        $path1 = "UPLOADS/Home/";
        $path2 = "UPLOADS/Breakfast/";
        $path3 = "UPLOADS/Spider/";
	        $scan1 = scandir($path1);
        $scan2 = scandir($path2);
        $scan3 = scandir($path3);
	        $count1 = count($scan1) - 3;
        $count2 = count($scan2) - 3;
        $count3 = count($scan3) - 3;
	        if($count1 > 0) {
            header('location:exist.html');
        }elseif ($count2 > 0) {
            header('location:exist.html');
        }elseif ($count3 > 0) {
            header('location:exist.html');
        }
	

But other site, it works:

	$scan = scandir($path);
$count = count($scan) - 3;
	echo $count;
	if($count > 1){
    echo "Hello yourself!<br />";
}
	

Anyone will help will be appreciate! Thank you!

Gary

Link to comment
Share on other sites

Hi gw1500se,

 

This part is not work to not send to other site name 'exist.html' if php find one video is inside folder in server. I notice server is still taking uploading file when file is exist already in folder. I'm trying to stop upload file into folder when there is one video or document inside folder already.

Link to comment
Share on other sites

All right gw1500se,

Here my full code, but it is three file different. one html and two php. I use php file to upload video, I use one html to let anyone know (display on monitor) that file is exist in folder. 

upload file first:

	<?php
	?>
<!doctype html>
        <html>
        <head>
            <link href="css/upload.css" rel="stylesheet" type="text/css">
        </head>
        <body>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            <table class="table">
                <tr>
                    <td colspan="2" align="center" class="children"><label for="file1">Children:</label></td>
                </tr>
                <tr>
                    <td>First Name:</td>
                    <td><input type="text" name="first" required></td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td><input type="text" name="last" required></td>
                </tr>
                <tr>
                    <td>School Name:</td>
                    <td><input type="text" name="school" required></td>
                </tr>
                <tr>
                    <td>Grade:</td>
                    <td><select name="grade">
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                            <option value="6">6</option>
                            <option value="7">7</option>
                        </select></td>
                </tr>
                <tr>
                    <td>Age:</td>
                    <td><input type="text" name="age" required></td>
                </tr>
                <tr>
                    <td>Test Video:</td>
                    <td><input type="radio" name="videos" value="Home">Home Alone</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><input type="radio" name="videos" value="Breakfast">Tiffany Breakfast</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><input type="radio" name="videos" value="Spider">Spider</td>
                </tr>
                <tr>
                    <td class="lowerfont">Upload your video</td>
                    <td><input type="file" name="file1" id="file1"></td>
                </tr>
	                <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="video" value="Upload your video"></td>
                </tr>
            </table>
        </form>
        <h2><a href="deletevideo.php" style="margin-left: 550px; color: white; text-decoration: none;">Go to Delete
                Video site</a></h2>
        <h2><a href="list.html" style="margin-left: 550px; color: white; text-decoration: none;">Return to list site</a>
        </h2>
        <div class="check"></div>
        </body>
        </html>
	

now, run to upload site...

[/php]

	<?php
	require ("access.php");
	// if the form was posted,    process the upload
if ($_POST['video']) {
	        $path1 = "UPLOADS/Home/";
        $path2 = "UPLOADS/Breakfast/";
        $path3 = "UPLOADS/Spider/";
	        $scan1 = scandir($path1);
        $scan2 = scandir($path2);
        $scan3 = scandir($path3);
	        $count1 = count($scan1) - 3;
        $count2 = count($scan2) - 3;
        $count3 = count($scan3) - 3;
	        if($count1 !== 0) {
            header('location:exist.html');
        }elseif ($count2 !== 0) {
            header('location:exist.html');
        }elseif ($count3 !== 0) {
            header('location:exist.html');
        }
	    $first = $_POST['first'];
    $last = $_POST['last'];
    $school = $_POST['school'];
    $age = $_POST['age'];
    $grade = $_POST['grade'];
    $option = $_POST['videos'];
	    if ($option == "Home") {
        $fileToMove = $_FILES['file1']['tmp_name'];
        $destination = "UPLOADS/Home/" . $_FILES['file1']['name'];
    } elseif ($option == "Breakfast") {
        $fileToMove = $_FILES['file1']['tmp_name'];
        $destination = "UPLOADS/Breakfast/" . $_FILES['file1']['name'];
    } elseif ($option == "Spider") {
        $fileToMove = $_FILES['file1']['tmp_name'];
        $destination = "UPLOADS/Spider/" . $_FILES['file1']['name'];
    }
	        if ($first && $last && $school && $age && $grade && $option) {
            $GaryDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $exec = $GaryDB->prepare("insert into Children (`FirstName`, `LastName`, `School`, `Grade`, `Age`) values (:firstN, :lastN, :school, :grade, :age)");
	            $exec->bindValue(':firstN', $first, PDO::PARAM_STR);
            $exec->bindValue(':lastN', $last, PDO::PARAM_STR);
            $exec->bindValue(':school', $school, PDO::PARAM_STR);
            $exec->bindValue(':grade', $grade, PDO::PARAM_STR);
            $exec->bindValue(':age', $age, PDO::PARAM_STR);
            $exec->execute();
	            if (move_uploaded_file($fileToMove, $destination)) {
                header('location: uploaded.html');
            } else {
                header('location: failupload.html');
            }
        }
    }
?>
	

Now, exist.html...

	<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Exist video</title>
    <link href="css/upload.css" rel="stylesheet" type="text/css">
</head>
<body>
<h2>There is video inside, you need to delete it to first.</h2>
<h2><a href="deletevideo.php">Delete site</a></h2>
</body>
</html>
	

that's my full code in html and php

Link to comment
Share on other sites

Ginerjm is right. Your code does not determine if a specific filename exists. You need to rewrite it, using much simpler code that just checks to see if that filename (is_file) exists. It is also a bit confusing whether or not the same filename can exist in the different directories you are checking.

Link to comment
Share on other sites

also be sure to use exit; after a header redirect or the rest of the php file will be executed.

$count3 = count($scan3) - 3;
	        if($count1 !== 0) {
            header('location:exist.html'); exit;
        }elseif ($count2 !== 0) {
            header('location:exist.html'); exit;
        }elseif ($count3 !== 0) {
            header('location:exist.html'); exit;
        }

 

Link to comment
Share on other sites

Or you could write your code like this:

$count3 = count($scan3) - 3;
if($count1 !== 0) 
{
	header('location:exist.html'); 
	exit;
}
elseif ($count2 !== 0) 
{
	header('location:exist.html'); 
	exit;
}
elseif ($count3 !== 0) 
{
	header('location:exist.html'); 
	exit;
}

While the tabs in this forum are a bit wider than I would like to see, writing your code in simple straight one-command lines is much better to read.  My personal choice is to use the curly braces each on their own line as well - again to make it easy on the eyes.  NEVER put 2 commands on one line.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.