Jump to content

Get Current Directory Errors ?


phpsane

Recommended Posts

Folks,

 

Check this simple form and it's processor out ...



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>File Upload with PHP</title>
</head>
<body>
    <form action="fileUpload.php" method="post" enctype="multipart/form-data">
        Upload a File:
        <input type="file" name="myfile" id="fileToUpload">
        <input type="submit" name="submit" value="Upload File Now" >
    </form>
</body>
</html>

 

<?php
    $currentDir = getcwd();
    $uploadDirectory = "/uploads/";

    $errors = []; // Store all foreseen and unforseen errors here

    $fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions

    $fileName = $_FILES['myfile']['name'];
    $fileSize = $_FILES['myfile']['size'];
    $fileTmpName  = $_FILES['myfile']['tmp_name'];
    $fileType = $_FILES['myfile']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));

    $uploadPath = $currentDir . $uploadDirectory . basename($fileName); 

    if (isset($_POST['submit'])) {

        if (! in_array($fileExtension,$fileExtensions)) {
            $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
        }

        if ($fileSize > 2000000) {
            $errors[] = "This file is more than 2MB. Sorry, it has to be less than or equal to 2MB";
        }

        if (empty($errors)) {
            $didUpload = move_uploaded_file($fileTmpName, $uploadPath);

            if ($didUpload) {
                echo "The file " . basename($fileName) . " has been uploaded";
            } else {
                echo "An error occurred somewhere. Try again or contact the admin";
            }
        } else {
            foreach ($errors as $error) {
                echo $error . "These are the errors" . "\n";
            }
        }
    }


?>

[?php]

 

I get these errors ...

Why I get them when 'myfile' has been defined ? Script should know the 'myfile' is the name of the input field in the form. Script should auto define it. Right ? Anything to do with getting the current directory or what ?

Notice: Undefined index: myfile in C:\xampp\htdocs\test\upload_file_SHORT.php on line 26

Notice: Undefined index: myfile in C:\xampp\htdocs\test\upload_file_SHORT.php on line 27

Notice: Undefined index: myfile in C:\xampp\htdocs\test\upload_file_SHORT.php on line 28

Notice: Undefined index: myfile in C:\xampp\htdocs\test\upload_file_SHORT.php on line 29

Notice: Only variables should be passed by reference in C:\xampp\htdocs\test\upload_file_SHORT.php on line 30

 

Link to comment
Share on other sites

Folks,

 

Oops! Messed up my original post and so here it goes again ...

 

Check this simple form and it's processor out ...

	<?php
    $currentDir = getcwd();
    $uploadDirectory = "/uploads/";
	    $errors = []; // Store all foreseen and unforseen errors here
	    $fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions
	    $fileName = $_FILES['myfile']['name'];
    $fileSize = $_FILES['myfile']['size'];
    $fileTmpName  = $_FILES['myfile']['tmp_name'];
    $fileType = $_FILES['myfile']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));
	    $uploadPath = $currentDir . $uploadDirectory . basename($fileName); 
	    if (isset($_POST['submit'])) {
	        if (! in_array($fileExtension,$fileExtensions)) {
            $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
        }
	        if ($fileSize > 2000000) {
            $errors[] = "This file is more than 2MB. Sorry, it has to be less than or equal to 2MB";
        }
	        if (empty($errors)) {
            $didUpload = move_uploaded_file($fileTmpName, $uploadPath);
	            if ($didUpload) {
                echo "The file " . basename($fileName) . " has been uploaded";
            } else {
                echo "An error occurred somewhere. Try again or contact the admin";
            }
        } else {
            foreach ($errors as $error) {
                echo $error . "These are the errors" . "\n";
            }
        }
    }
	
?>
	<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>File Upload with PHP</title>
</head>
<body>
    <form action="fileUpload.php" method="post" enctype="multipart/form-data">
        Upload a File:
        <input type="file" name="myfile" id="fileToUpload">
        <input type="submit" name="submit" value="Upload File Now" >
    </form>
</body>
</html>
	

I get these errors ...

Why I get them when 'myfile' has been defined ? Script should know the 'myfile' is the name of the input field in the form. Script should auto define it. Right ? Anything to do with getting the current directory or what ?

Notice: Undefined index: myfile in C:\xampp\htdocs\test\upload_file_SHORT.php on line 26

Notice: Undefined index: myfile in C:\xampp\htdocs\test\upload_file_SHORT.php on line 27

Notice: Undefined index: myfile in C:\xampp\htdocs\test\upload_file_SHORT.php on line 28

Notice: Undefined index: myfile in C:\xampp\htdocs\test\upload_file_SHORT.php on line 29

Notice: Only variables should be passed by reference in C:\xampp\htdocs\test\upload_file_SHORT.php on line 30

Link to comment
Share on other sites

UIman/phpsane, after over two years you should very well know that Php runs from the top down and that you need to check if there was a POST request before your processing code code runs. undefined index errors are first day in Php noob errors. After all this time you STILL cant grasp the most basic of basics. Truley unbelievable! :facewall:

Link to comment
Share on other sites

Why do we continue to tolerate a member who genuinely refuses to take the business of programming seriously and Read ANY FM? This person obviously doesn't take learning seriously and is proud of his self-designed supposed 'learning-method'.  He tries to copy and execute complex scripts that he can't decipher and wonders why they don't work.  If he took just one example from a book written by a real author (and not a newbie-authored bit of script he finds somewhere) and tried to execute it and understand it and learn from that experience he would be many steps ahead of where he is today.  He seems genuinely proud of what he is doing and then proves how hopelessly un-learned he is when he posts stuff (that he even has trouble doing!)  like this that is so patently flawed that ANYBODY should be able to see it for what it is.

Mr. PHP(in)SANE - Rule #1 of php writing.  Write ONLY php at the beginning of your script until you have done everything that PHP needs to do.  Only then begin to write HTML.  Remember - you cannot refer to anything from a page that hasn't yet been sent to the client which would remove the current  coding "problem" you are having.

And please try to keep your posts in a serious vein.  Your habit of meaningless gibber-jabber is annoying and completely unnecessary.  You come here for serious help, so try and be serious.

You would never have worked for me.

Link to comment
Share on other sites

On 9/3/2018 at 3:11 AM, benanamen said:

UIman/phpsane, after over two years you should very well know that Php runs from the top down and that you need to check if there was a POST request before your processing code code runs. undefined index errors are first day in Php noob errors. After all this time you STILL cant grasp the most basic of basics. Truley unbelievable! :facewall:

Banenanamen,

Thanks man! You reminded me something I forgot.

Look how I fixed it ...

Notice my comment where I speak about the ADDITION.

	<?php
	    /*
    $currentDir = getcwd();
    $uploadDirectory = "/uploads/";
	    $errors = []; // Store all foreseen and unforseen errors here
	    $fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions
	    $fileName = $_FILES['myfile']['name'];
    $fileSize = $_FILES['myfile']['size'];
    $fileTmpName  = $_FILES['myfile']['tmp_name'];
    $fileType = $_FILES['myfile']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));
	    $uploadPath = $currentDir . $uploadDirectory . basename($fileName);     
    */
	    if (isset($_POST['submit'])) {
        
    //ADDED HERE FROM ABOVE
    //ADDITION STARTS HERE    
        $currentDir = getcwd();
    $uploadDirectory = "/uploads/";
	    $errors = []; // Store all foreseen and unforseen errors here
	    $fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions
	    $fileName = $_FILES['myfile']['name'];
    $fileSize = $_FILES['myfile']['size'];
    $fileTmpName  = $_FILES['myfile']['tmp_name'];
    $fileType = $_FILES['myfile']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));
    //ADDITION ENDS HERE
	    $uploadPath = $currentDir . $uploadDirectory . basename($fileName); 
	        if (! in_array($fileExtension,$fileExtensions)) {
            $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
        }
	        if ($fileSize > 2000000) {
            $errors[] = "This file is more than 2MB. Sorry, it has to be less than or equal to 2MB";
        }
	        if (empty($errors)) {
            $didUpload = move_uploaded_file($fileTmpName, $uploadPath);
	            if ($didUpload) {
                echo "The file " . basename($fileName) . " has been uploaded";
            } else {
                echo "An error occurred somewhere. Try again or contact the admin";
            }
        } else {
            foreach ($errors as $error) {
                echo $error . "These are the errors" . "\n";
            }
        }
    }
?>
	<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>File Upload with PHP</title>
</head>
<body>
    <form action="fileUpload.php" method="post" enctype="multipart/form-data">
        Upload a File:
        <input type="file" name="myfile" id="fileToUpload">
        <input type="submit" name="submit" value="Upload File Now" >
    </form>
</body>
</html>
	

Link to comment
Share on other sites

On 9/4/2018 at 10:33 PM, benanamen said:

I guess you also forgot you are supposed to check the REQUEST METHOD and not the name of a button. :facewall:

You are right again. I forgot what tango Force, your mate, taught at codingforums.com (British forum).

I guess you want me to replace this:

if (isset($_POST['submit']))

with this:

if($_SERVER["REQUEST_METHOD"] == "POST") 

Benanamen, can you do me a small favour ? Damn internet is full of outdated tutorials:

https://cloudinary.com/blog/file_upload_with_php

That is where I got the code from. How-about you find an upto date one and provide the link here so newbies can learn from an upto date one ?

Or, do you reckon the following amended code is ok now after I have updated it to the following from the code you see on the above link ?

	<?php
	    /* 
    $currentDir = getcwd();
    $uploadDirectory = "/uploads/";
	    $errors = []; // Store all foreseen and unforseen errors here
	    $fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions
	    $fileName = $_FILES['myfile']['name'];
    $fileSize = $_FILES['myfile']['size'];
    $fileTmpName  = $_FILES['myfile']['tmp_name'];
    $fileType = $_FILES['myfile']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));
	    $uploadPath = $currentDir . $uploadDirectory . basename($fileName);     
    */
    // In the tutorial, the above 10 lines are up there as you see them. Due to getting undefined index error, I am taking them 10 lines below inside the "if($_SERVER["REQUEST_METHOD"] == "POST") { "
	    //if (isset($_POST['submit']) Commendted-out the putdated code that was found in the tutorial.
    if($_SERVER["REQUEST_METHOD"] == "POST") { //Changed to this from this outdated code you see on the above line... "if (isset($_POST['submit'])"
        
    //ADDED HERE FROM ABOVE. ADDITION STARTS HERE    
    $currentDir = getcwd();
    $uploadDirectory = "uploads/videos/id_verifications/";
	    $errors = []; // Store all foreseen and unforseen errors here
	    $fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions
	    $fileName = $_FILES['myfile']['name'];
    $fileSize = $_FILES['myfile']['size'];
    $fileTmpName  = $_FILES['myfile']['tmp_name'];
    $fileType = $_FILES['myfile']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));
    //ADDITION ENDS HERE
	    $uploadPath = $currentDir . $uploadDirectory . basename($fileName); 
	        if (! in_array($fileExtension,$fileExtensions)) {
            $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
        }
	        if ($fileSize > 2000000) {
            $errors[] = "This file is more than 2MB. Sorry, it has to be less than or equal to 2MB";
        }
	        if (empty($errors)) {
            $didUpload = move_uploaded_file($fileTmpName, $uploadPath);
	            if ($didUpload) {
                echo "The file " . basename($fileName) . " has been uploaded";
            } else {
                echo "An error occurred somewhere. Try again or contact the admin";
            }
        } else {
            foreach ($errors as $error) {
                echo $error . "These are the errors" . "\n";
            }
        }
    }
?>
	<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>File Upload with PHP</title>
</head>
<body>
    <form action="fileUpload.php" method="post" enctype="multipart/form-data">
        Upload a File:
        <input type="file" name="myfile" id="fileToUpload">
        <input type="submit" name="submit" value="Upload File Now" >
    </form>
</body>
</html>
	

Is everything ok now on the above amended code ? ?

 

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.