JukEboX Posted July 6, 2008 Share Posted July 6, 2008 Ok I have a homepage. Inside the homepage directory I have an administration directory where admins work. I am trying to add a news area for them to add news. Here is teh new form. <form action="NewsInputSubmit.php" onsubmit="return validate_form(this)" method="post"> <table border="0" cellpadding="0" cellspacing="0" class="Form" align="center"> <tr> <td class="FormQ">Topic:</td> <td class="FormIn"><input type="text" name="Topic" size="40"/></td> </tr> <tr> <td class="FormQ">Poster:</td> <td class="FormIn"><input type="text" name="Poster" size="40"/></td> </tr> <tr> <td class="FormQ">Article:</td> <td class="FormIn"><textarea name="Text" class="Text" rows="6" cols="42"></textarea></td> </tr> <tr> <td class="FormQ">Subject:</td> <td class="FormIn"><select name="Subject"> <option value=" "> </option> <option value="All">All</option> <?php $p = 0; while ($p < $gamenum) { $GameName = mysql_result($gameinfo,$p,"GameName"); echo "<option value='$GameName'>$GameName</option>"; $p++; }; ?> </select></td> </tr> <tr> <td class="FormQ">Type:</td> <td class="FormIn"><select name="Type"> <option value=" "> </option> <option value="A">Article</option> <option value="N">News</option> </select></td> </tr> <tr> <td class="FormQ">Exact Name of Picture:</td> <td class="FormIn"><input type="text" name="Picture" size="40"/></td> </tr> <tr> <td class="FormQ" colspan="2">Upload News Picture: <input type="file" name="uploaded" /></td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td colspan="4"align="center"><input type="submit" value="Submit Message" /></td> </tr> </table> </form> NO then on the submit page I have this for my PHP code to upload the picture $target = "/public_html/mysite/NewsPics/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; 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."; } Angrilly enough I am not sure its working cause when I hit submit it does not pull the picture to the specified folder which is in the Main web page folder. SO the setup is Main Main>NewsSubmitDIR Main>PictureLocationDIR Can someone tell me what I am doing Wrong? I mean this is a pretty simple code to work but its just not working. Thanks. Quote Link to comment Share on other sites More sharing options...
nubby Posted July 6, 2008 Share Posted July 6, 2008 PHP: $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } Looks like the first line needs to be target_path Try this. Or try here: http://www.tizag.com/phpT/fileupload.php That is where I got it from, but got to go so I cannot help out completely right now. Sorry. Quote Link to comment Share on other sites More sharing options...
JukEboX Posted July 6, 2008 Author Share Posted July 6, 2008 I have tried that but I still am not having any luck. I can't get the file to the correct location. Either that or the file is not going in. Anyone have any ideas? Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 6, 2008 Share Posted July 6, 2008 I'm pretty sure when you use the $_FILE variable, you have to have " enctype="multipart/form-data" " in the form tag. Give that a try. Otherwise, the file upload script looks to be correct Quote Link to comment Share on other sites More sharing options...
TransmogriBenno Posted July 6, 2008 Share Posted July 6, 2008 1. You should check that the file has actually been uploaded first before you try to move it, e.g. is_uploaded_file. 2. The above is failing because you are sending through a plain text form with no files attached, try: <form enctype="multipart/form-data" ...> 3. You need to be sure that the user that the script runs as (quite often 'nobody', depending on the server's configuration) has permissions for the directory you're trying to store the file in. Quote Link to comment Share on other sites More sharing options...
JukEboX Posted July 6, 2008 Author Share Posted July 6, 2008 I am uploading the file in an administrative area which is in a folder inside the main directory protected by htaccess. I want to move the file from there to a open folder inside the main directory. I have updated teh code. How do I tell it to go to taht other directory from the administrative directory? Form <form action="NewsInputSubmit.php" enctype="multipart/form-data" onsubmit="return validate_form(this)" method="post"> <table border="0" cellpadding="0" cellspacing="0" class="Form" align="center"> <tr> <td class="FormQ">Topic:</td> <td class="FormIn"><input type="text" name="Topic" size="40"/></td> </tr> <tr> <td class="FormQ">Poster:</td> <td class="FormIn"><input type="text" name="Poster" size="40"/></td> </tr> <tr> <td class="FormQ">Article:</td> <td class="FormIn"><textarea name="Text" class="Text" rows="6" cols="42"></textarea></td> </tr> <tr> <td class="FormQ">Subject:</td> <td class="FormIn"><select name="Subject"> <option value=" "> </option> <option value="All">All</option> <?php $p = 0; while ($p < $gamenum) { $GameName = mysql_result($gameinfo,$p,"GameName"); echo "<option value='$GameName'>$GameName</option>"; $p++; }; ?> </select></td> </tr> <tr> <td class="FormQ">Type:</td> <td class="FormIn"><select name="Type"> <option value=" "> </option> <option value="A">Article</option> <option value="N">News</option> </select></td> </tr> <tr> <td class="FormQ">Exact Name of Picture:</td> <td class="FormIn"><input type="text" name="Picture" size="40"/></td> </tr> <tr> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <td class="FormQ" colspan="2">Upload News Picture: <input type="file" name="uploadedfile" /></td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td colspan="4"align="center"><input type="submit" value="Submit Message" /></td> </tr> </table> </form> Update script on the submit page: $target = "//public_html/mysite/NewsPics/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); $_FILES['uploadedfile']['tmp_name']; If(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?> I am pretty sure with the code this way it does not work. I have tried it many times. Quote Link to comment Share on other sites More sharing options...
JukEboX Posted July 6, 2008 Author Share Posted July 6, 2008 I keep getting: There was an error uploading the file, please try again!"; No matter what I change. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 6, 2008 Share Posted July 6, 2008 Ah ha. the problem may be... you're starting your insert from "//public_html... and so on. In my upload script, I have the target set to start from my current directory So if my file is a jpeg, and I want it to go into "images" and it be named "image1234.jpg", I'd make the $target variable: "images/image1234.jpg" and thats it. I'm pretty sure that's where your problem is. Also, I think you might need to specify the max file size(but it might not be important) <input type="hidden" name="MAX_FILE_SIZE" value="10000000000"> But the server may have limitations that are lower than the values specified. Also, how big is the file you're trying to upload? Quote Link to comment Share on other sites More sharing options...
JukEboX Posted July 6, 2008 Author Share Posted July 6, 2008 thanks danny B. on my last test it uploaded ok but it still says it did not go through but it did. Also the form for the upload is in Main>Panel I want the picture to go to Main>Pictures How do i set then if its off the same directory but in a different folder? Quote Link to comment Share on other sites More sharing options...
JukEboX Posted July 6, 2008 Author Share Posted July 6, 2008 actaully scratch that. the file uploaded great. Just how do I set the folder to the pictures folder if the folder its uploading to is off the main directory and the upload script is in another file off the main directory. Main > Panel > <-- Where the upload script is Main > Pictures > <-- Where the file needs to go Any ideas? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 6, 2008 Share Posted July 6, 2008 ../Pictures Quote Link to comment Share on other sites More sharing options...
JukEboX Posted July 6, 2008 Author Share Posted July 6, 2008 I don't think thats right. It jsut went into the the same directory as the upload page. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 6, 2008 Share Posted July 6, 2008 Let me get this correct: main/panel/upload.php <- Upload script (random name, I don't know if it's actually upload.php) main/pictures/somepicture.jpg <- Example path where you want picture In upload.php, you'd use: ../pictures/{$_FILES['name']} And it's probably case sensitive (if you use Linux, it is), so watch out. Quote Link to comment Share on other sites More sharing options...
JukEboX Posted July 6, 2008 Author Share Posted July 6, 2008 This is weird cause it keeps going into the same directoy as the upload.php . Something I am doing wrong with my script? $target = "../NewsPics/{$_FILES['name']}"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); $_FILES['uploadedfile']['tmp_name']; if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } Quote Link to comment Share on other sites More sharing options...
bothwell Posted July 6, 2008 Share Posted July 6, 2008 This is weird cause it keeps going into the same directoy as the upload.php . Something I am doing wrong with my script? $target = "../NewsPics/{$_FILES['name']}"; Is $target called anywhere after it's been set? Quote Link to comment Share on other sites More sharing options...
JukEboX Posted July 6, 2008 Author Share Posted July 6, 2008 No it does not. I am so confused. Quote Link to comment Share on other sites More sharing options...
bothwell Posted July 6, 2008 Share Posted July 6, 2008 No it does not. I am so confused. Try: $target_path = $target . basename( $_FILES['uploadedfile']['name']); ? Quote Link to comment Share on other sites More sharing options...
JukEboX Posted July 6, 2008 Author Share Posted July 6, 2008 Great that worked!!!! ANy idea why it wasn't working before? Quote Link to comment Share on other sites More sharing options...
bothwell Posted July 6, 2008 Share Posted July 6, 2008 Great that worked!!!! ANy idea why it wasn't working before? Yes - you set the filepath as $target and then created a new variable called $target_path to hold the filepath and filename, but referred the filepath part back into itself ($target_path = $target_path . filenamestuff ) 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.