May 14, 201115 yr I was trying this code for uploading files into the folder "upload/" <?php if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } ?> But I get some kind of "Undefined index:" blah blah blah... many times and nothing happens. What should I do? Thanks in advance.
May 14, 201115 yr Author I presume there should be no problem in my html document. Anyway here's the code. <html> <head> <title> Upload </title> </head> <body> <p> <form action="upload.php" method="post" enctype="mutlipart/form-data"> <input type="file" name="file" id="file"/> <br/> <input type="submit" name="submit" value="submit"/> </form> </p> </body> </html>
May 14, 201115 yr I presume there should be no problem in my html document. Anyway here's the code. <html> <head> <title> Upload </title> </head> <body> <p> <form action="upload.php" method="post" enctype="mutlipart/form-data"> <input type="file" name="file" id="file"/> <br/> <input type="submit" name="submit" value="submit"/> </form> </p> </body> </html> to prevent undefined indexes i'd create a function for files like this function doFile($file,$type){ if(isset($_FILES[$file][$type])){ return $_FILES[$file][$type]; } } //Then use like this doFile('file','type'); doFile('file','name'); doFile('file','size'); //ect..
May 14, 201115 yr So where are you checking the file extensions? or are you allowing anyone to upload anything via the php script not a very clever plan.... Im not going to write this code for you but for example. $allowedformats = array('.html', '.sql', '.gif', ); Then link it up with the rest of the code and ask it to check in the array ect. Thanks Cobra
May 14, 201115 yr Nope not being critical and as stated it was an "example" of what could be done to improve the current bit of code, Is this not a community where people suggest things and help to improve what others post? back at you
May 14, 201115 yr Nope not being critical and as stated it was an "example" of what could be done to improve the current bit of code, Is this not a community where people suggest things and help to improve what others post? back at you I have to agree, if he isn't told about these things now, while he's learning then it will come back and bite him on the ass later. Cobra did say it was only an example and without seeing what his code would do with that array, rallport, you really can't say how effective it will be.
May 14, 201115 yr I have to agree, if he isn't told about these things now, while he's learning then it will come back and bite him on the ass later. Cobra did say it was only an example and without seeing what his code would do with that array, rallport, you really can't say how effective it will be. rallport is referring to the fact that file types can be faked, its a security issue, so yes im pretty sure as a developer he can say how effective it will be Edited May 14, 201115 yr by webdesigner93
May 14, 201115 yr How can he say how effect code is that he cannot see? No matter what file extensions you are going to allow or reject and no matter how you are going to check them you are always going to have to tell the script the ones to allow/reject. You CANNOT tell anything about how effective the script is based on that array.
May 14, 201115 yr How can he say how effect code is that he cannot see? No matter what file extensions you are going to allow or reject and no matter how you are going to check them you are always going to have to tell the script the ones to allow/reject. You CANNOT tell anything about how effective the script is based on that array. Grabs another can of stella and gets comfortable
May 14, 201115 yr How can he say how effect code is that he cannot see? No matter what file extensions you are going to allow or reject and no matter how you are going to check them you are always going to have to tell the script the ones to allow/reject. You CANNOT tell anything about how effective the script is based on that array. Exactly I posted a snippet not my full upload script. As I have stated it was an example on how he could improve his script, With a simple array it can be improved there for I shared my thoughts on how to improve the above code. As a way to contribute something to this community Nothing more. Cheers Cobra
May 14, 201115 yr Grabs another can of stella and gets comfortable Already got a fosters ran out of stella so its all good
May 14, 201115 yr Already got a fosters ran out of stella so its all good I'm eating pot noddle - guess that will have to do! (to partially save my "man" status, it IS a bombay badboy one!)
May 14, 201115 yr I'm eating pot noddle - guess that will have to do! (to partially save my "man" status, it IS a bombay badboy one!) Lol least you got a pot noddle all I have is a crisp I coded last week :/
May 23, 201115 yr I'm with webdesigner93 on this one- Checking that the array value is set before trying to process it. I even tend to wrap that up with an if (!isset($_FILES)) { echo "No file information found"; } else { ... } first to ensure the mechanism is working, but that might be a bit of an over kill!
May 23, 201115 yr I'm with webdesigner93 on this one- Checking that the array value is set before trying to process it. I even tend to wrap that up with an if (!isset($_FILES)) { echo "No file information found"; } else { ... } first to ensure the mechanism is working, but that might be a bit of an over kill! php file upload code
Create an account or sign in to comment