Jump to content

Upload problem driving me crazy


steve490

Recommended Posts

hello there,

 

Can any tell me why this upload code isn't working. What its suppose to do is upload a picture to a server and its id to a database with the users session data. its uploading to the database but not the the server folder heres the code:

 


$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = '******';
mysql_select_db($dbname);

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$pic = ($_FILES['uploadedfile']['name']);

mysql_query ("INSERT INTO img (name, email) VALUES ('uploads/$pic','$_SESSION[email]')");  

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded to " . $target_path;
} else{
    echo "There was an error uploading the file, please try again!";
}



 

here's the html

 


<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />

 

cheers in advance

Link to comment
Share on other sites

Okay, it really does not seem to be a code problem to me. It might be a few other things. Try these and see if it helps:

 

1.) The folder "uploads" might need the permissions to be changed. Try setting them 0755 or 0777.

2.) if you can, depending on your privileges with your server, try putting the uploads directory outside of the web root. So instead of the variable $tagert_path = 'uploads/' it would be written like this: $target_path = '../uploads/'

3.) If you cannot change the path of the uploads directory, try add a forward slash in front of the directory like this: $target_path = '/uploads/'

 

Try those and hopefully one of those work for you.

Link to comment
Share on other sites

We cannot tell you why the upload is failing because your code has no logic in it to test for any of the possible upload errors and tell you which one it causing the upload to fail. See this recent thread for the things you must test before you attempt to access any of the uploaded file information - http://www.phpfreaks.com/forums/index.php/topic,286370.msg1357855.html#msg1357855

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.