antwonw Posted December 30, 2007 Share Posted December 30, 2007 Hey, I am trying to write this upload script for my website. But so far there are two things I don't like. 1. They are two separate files 'form.html' and 'uploader.php' 2. The stupid thing isn't uploading in any case What I would like to do... 1. Get it to work 2. Put it in one file Here is the code for the 'form.html' Upload: <form action="uploader.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" size="30"> <input type="submit" value="Upload File"> </form> And here is the code for my 'uploader.php' <?php if($file_name !="") { copy ("$file", "../homes/$file_name") or die("Could not copy file"); } else { die("No file specified"); } ?> So can anyone tell me... 1. What is wrong and why it won't upload 2. How I can merge them together Thanks, Antwonw Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 30, 2007 Share Posted December 30, 2007 try this <?php echo' <form enctype="multipart/form-data" action="__URL__" method="POST"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="3000000" /> <!-- Name of input element determines name in $_FILES array --> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /> </form>'; $uploaddir = '/tp-images/Image/'; // file directory... $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "file was not upload ".$_FILES['userfile']['name'].""; } ?> Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 30, 2007 Share Posted December 30, 2007 I would recommend using the example in the manual: http://www.php.net/features.file-upload Specifically, example two. That should get your file uploaded. To use a single file, do some thing like this: <?php if (count($_FILES) > 0) { /* code to handle file upload (see php manual) */ //pseudo code: if ($file_uploaded_successfully == true) { $message = "Your file has been uploaded"; } else { $message = "There was an error uploading the file"; } } ?> <!-- Put your html upload form here --> <?php echo $message; ?> Make sure that the form submits to the same page it's on. The premise is to check to see if the form was submitted ( "if (count($_FILES) > 0) ...' ), if it has, process the upload and set a message to display for the user. Quote Link to comment Share on other sites More sharing options...
antwonw Posted December 30, 2007 Author Share Posted December 30, 2007 I finally got it to work with this... Upload File: <form name=upload action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" size="30"> <input type="submit" value="Upload"> </form> <?php $DestinationDir = "/xampp/htdocs/upload/"; $DestinationFile = $DestinationDir . $_FILES['file']['name']; If (move_uploaded_file($_FILES['file']['tmp_name'], $DestinationFile)) { Echo "File uploaded successfully."; }Else{ Echo $_FILES['file']['error']; } ?> Only problem is I am wanting to add it to another page and I used this... <td> <?php include( "upload.php" ); ?> </td> Well in the other page it doesn't upload. But if I got directly to my 'upload.php' file it does upload. What am I doing wrong? Thanks, Antwonw Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 30, 2007 Share Posted December 30, 2007 // add this to the other page... Upload File: <form name=upload action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" size="30"> <input type="submit" value="Upload"> </form> call this one upload.php <?php $DestinationDir = "/xampp/htdocs/upload/"; $DestinationFile = $DestinationDir . $_FILES['file']['name']; If (move_uploaded_file($_FILES['file']['tmp_name'], $DestinationFile)) { Echo "File uploaded successfully."; // redirect user here to the main page }Else{ Echo $_FILES['file']['error']; } ?> Quote Link to comment Share on other sites More sharing options...
antwonw Posted December 30, 2007 Author Share Posted December 30, 2007 Thanks. I got it to work. 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.