Jump to content

Picture Upload


JukEboX

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!";
	}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 )

 

:)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.