0

i am building a simple file upload function. the uploaded file should go/store into the "uploads" folder. but they wont go in the folder. Any help?

Below are the code:

<?php

include("connect.php");
$file_name = trim($_POST['file_name']);
$file_size = $_FILES['size'];
$file_type = $_FILES['type'];
$description = trim($_POST['description']);
$date_entered = trim($_POST['date_entered']);

$query = "INSERT INTO uploads (id, file_name, file_size, file_type, description, date_entered)
VALUES ('', '$file_name', '$file_size', '$file_type', '$description', '$date_entered')";
$results = mysql_query($query);

if ($results)
{
  echo "Details added.";

  move_uploaded_file($_FILES[$file_name]['tmp_name'], "uploads/$id");
}

mysql_close();
?>
1
  • Please use proper formatting and well formatted code. When you don't use any syntax highlighting or put all of your code on the same line, you are making people want to hurt you instead of helping you. Commented Jul 13, 2011 at 7:20

3 Answers 3

1

Change the move_uploaded_file call to this:

move_uploaded_file($_FILES['file_name']['tmp_name'], "uploads/$id");

Also make sure that the uploads/ folder is writable by the web server user. This is usually done by setting the permissions to 0777 using chmod in SSH or FTP.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. i just did that. what do you mean by "make sure that the uploads/ folder is writable by the web server user"?
PHP is often run as the same user as the web server, or at least some other user than yourself. Therefore you have to give it specific writing permissions using your FTP program. If you have your own server, use the chmod command over SSH. If you only have FTP access (e.g., shared hosting), right-click on the folder in your FTP program, choose properties or some similar option and look for a permissions tab. Give "everyone" or "other" writing permissions. If you are on shared hosting, contact their support if you are unsure on how to do this.
0

What folder is the uploads folder in? I assume it's [code folder]/uploads, but you never know.. Otherwise, is the query throwing an exception? Or returning false? Is "Details Added" printed?

Comments

0

Just because you are running a script in your website called somthing/directory/file.php doesnt make that "uploads" would be seen as something/directory/uploads/ ..

Also, check the permissions on the directory.

You are setting filename as a separate name rather than using the uploaded file name? Is this in your form (You didnt include the form for us to check)

I dont see you set $id anywhere?

Is the SQL working? move_uploaded_file returns true/false, you arent checking that the move was successful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.