tags:

views:

303

answers:

3

Does anyone know any good tutorial on how to upload a file with php and save the files path to a sql server?

+1  A: 

file upload is described in the php manual page. and storing a string in the database is very common task. Are you sure you really need a tutorial for the every practical case consists of several usual tasks?

Col. Shrapnel
+1  A: 

From http://www.w3schools.com/php/php_file_upload.asp

HTML

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

PHP

<?php
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {   
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; //<- This is it
      }
    }
?>

Note that to upload the file you need to specify the path to save the file. If you save the file you already know it path.

Ben
A: 

To upload a file you need at least a HTML POST form with multipart/form-data encoding. Therein you put an input type="file" field to browse the file and a submit button to submit the form.

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>

In the upload.php the uploaded file is accesible by $_FILES with the field name as key.

$file = $_FILES['file'];

You can get its name as follows:

$name = $file['name'];

You need to move it to a permanent location using move_uploaded_file(), else it will get lost:

$path = "/uploads/" . basename($name);
if (move_uploaded_file($file['tmp_name'], $path)) {
    // Move succeed.
} else {
    // Move failed. Possible duplicate?
}

You can store the path in database the usual way:

$sql = "INSERT INTO file (path) VALUES ('" . mysql_real_escape_string($path) . "')";
// ...
BalusC
Man it's BUGGY. To write tutorials, one need much more experience
Col. Shrapnel
@Col: Sorry, I can't help that you have had a bad day. But you're welcome :)
BalusC
My bad day is not an excuse for the lame mistakes in your code.
Col. Shrapnel
@Col: You downvoted and ranted on the answer instead of elaborating the problem in the answer. Well, then I can't do much to satisfy you. Should I maybe have added an errorcheck/nullcheck? That kind of things. Just say that so instead of ranting :)
BalusC
You take it too direct. I don't blame you for this as it seems very common habit here on SO. I am not talking of particular problem`s` in your answer. I am talking in general. It's not errors. It's just signs of inexperience. Being inexperienced, you just wrote it in pursue of precious rep, not real help. Why should I concern in what you don't?
Col. Shrapnel
@Col: Blub? But OK, you're welcome :)
BalusC
good, at least you corrected a typo :) I was hope 50k guy can do it without a hint :) Now think of "Possible duplicate" comment. It is just comment, yes, but it's totally incorrect and may confuse further readers.
Col. Shrapnel
and `$path = "/uploads/"` is VERY common mistake for the newbie developers. It is almost impossible to have uploads directory in the filesystem root in the real environment. Most likely it being confused with document root.
Col. Shrapnel
@Col: I usually use dedicated/colocated hosting where I have just full control over the file system. That's the normal practice in Java webapps as well, but you're indeed right with regard to the average PHP webapp/hosting.
BalusC
Just curious, what OS let you upload in the root folder? the only one I know is windows. But it's still bad design to place a directory directly into root folder, outside of application's directory, isn't it?
Col. Shrapnel
True. It was just a kickoff example :) Put them wherever you can write to and is easily to be related to the webapp. For even `/var/www/uploads` or so.
BalusC