jacko_162 Posted September 11, 2011 Share Posted September 11, 2011 I have a small form which executes a php file, and the executable php file. is there anyway to have it just using the 1 php file? form code: <form enctype="multipart/form-data" action="upload-exec.php" method="POST"> Please choose a file: <input name="uploaded" type="file" /><br /> <input type="submit" value="Upload" /> </form> uploader.php <?php $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; //This is our size condition if ($uploaded_size > 350000) { echo "Your file is too large.<br>"; $ok=0; } //This is our limit file type condition if ($uploaded_type =="text/php") { echo "No PHP files<br>"; $ok=0; } //Here we check that $ok was not set to 0 by an error if ($ok==0) { Echo "Sorry your file was not uploaded"; } //If everything is ok we try to upload it else { if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file."; } } ?> i did try using: if (isset($_POST['submit'])) { with no luck. Many thanks Quote Link to comment Share on other sites More sharing options...
voip03 Posted September 12, 2011 Share Posted September 12, 2011 you haven't name the submit button. <input type="submit" value="Upload" name="submit"/> Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 12, 2011 Share Posted September 12, 2011 <?php if(isset($_FILES['uploaded']['name'])) { $target = "upload/"; $fileName = basename($_FILES['uploaded']['name']); $target = $target . $fileName; $errors = array(); //This is our size condition if ($uploaded_size > 350000) { $errors[] = "Your file is too large."; } //This is our limit file type condition if ($uploaded_type=="text/php") { $errors[] = "No PHP files"; } //Here we check that no validation errors have occured if (count($errors)==0) { //Try to upload it if(!move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { $errors[] = "Sorry, there was a problem uploading your file."; } } //If no errors show confirmation message if(count($errors)==0) { echo "The file {$fileName} has been uploaded"; } else { //show error message echo "Sorry your file was not uploaded due to the following errors:<br>\n"; echo "<ul>\n"; foreach($errors as $error) { echo "<li>{$error}</li>\n"; } echo "</ul>\n"; } } else { //Show the form echo "<form enctype='multipart/form-data' action='' method='POST'>\n"; echo "Please choose a file: <input name='uploaded' type='file' /><br />\n"; echo "<input type='submit' value='Upload' />\n"; echo "</form>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted September 12, 2011 Author Share Posted September 12, 2011 <?php if(isset($_FILES['uploaded']['name'])) { $target = "upload/"; $fileName = basename($_FILES['uploaded']['name']); $target = $target . $fileName; $errors = array(); //This is our size condition if ($uploaded_size > 350000) { $errors[] = "Your file is too large."; } //This is our limit file type condition if ($uploaded_type=="text/php") { $errors[] = "No PHP files"; } //Here we check that no validation errors have occured if (count($errors)==0) { //Try to upload it if(!move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { $errors[] = "Sorry, there was a problem uploading your file."; } } //If no errors show confirmation message if(count($errors)==0) { echo "The file {$fileName} has been uploaded"; } else { //show error message echo "Sorry your file was not uploaded due to the following errors:<br>\n"; echo "<ul>\n"; foreach($errors as $error) { echo "<li>{$error}</li>\n"; } echo "</ul>\n"; } } else { //Show the form echo "<form enctype='multipart/form-data' action='' method='POST'>\n"; echo "Please choose a file: <input name='uploaded' type='file' /><br />\n"; echo "<input type='submit' value='Upload' />\n"; echo "</form>\n"; } ?> Code works loverly, how can i add a query in your above code to add the filename to the database and if possible can we generate a unique random number and rename said file to stop other files being overwritten? Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 13, 2011 Share Posted September 13, 2011 Well, you have to decide: should you copy the file first and then update the database or update the database first then move the file. I'd probably go with the former. Just add the code necessary as an else condition to the if() statement that uploads the file. As for adding a unique random number you would either have to check the available file names or check the file names stored in the database. Again, I would go with the former. I'd do a single check to see if there is an exact match of the file name. If no, move the file with the original name. If yes, they I would use glob() to find all the "like" file names. You would need to split the file name into the path/name part and the extension. Then find all the files matching the original with additional "text" something like $matches = glob($path.$name.'*'.$ext); Then just loop through till you find an available number to use. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.