Link to home
Start Free TrialLog in
Avatar of lienny
lienny

asked on

file upload and save path to mysql

hi,

Can someone help me with this?
I have a file upload script and it doesn't seem to be working correctly.
this is for a recipes database.
it's saving the name of the file, like shrimpmango.gif in the table, but isn't it suppose to save the path?
anyways, it's not saving the actual image to my uploads directory either. here's my script:

if(isset($bgid))
{
$folder = "/recipes/uploads/";
  @copy("$image" , "$folder/$image_name");
      $msg="lo";
      $sql="insert into recipe(instructions,ingredients,contributor,categoryid,title,upldate,description,prep,servings,image) values('$instructions','$ingredients','$contributor',$bgid,'$title',now(),'$description','$prep','$servings','$image_name')";
      $res=mysql_query($sql);
}

<form method="post" name="myform" action="<? print $PHP_SELF; ?>"  onsubmit="javascript: return checkifvalid();" enctype="multipart/form-data">
<tr>
                                 <td>Browse a File to Upload:</td>
                                 <td><input type="file" name="image" size=45></td>
                              </tr>
                    <tr>
                      <td colspan="2" height="17"> <p align="center">
                          <input type="submit" name="Submit" value="Add Recipe">
                      </td>
                    </tr>
</form>
Avatar of cLFlaVA
cLFlaVA

well, the path would be

"$folder/$image_name" according to your code above, yet you're only inserting $image_name into the database.
Avatar of lienny

ASKER

that still doesn't resolve the path name being stored in the db.  also, it doesn't save my file to the uploads directory.

Ok, let's start from the top

use this:
    # path to save the file to
    $folder = "/recipes/uploads";

    # get the temporary name of the file
    $image = $_FILES['image']['tmp_name'];

    # copy the temp file to the file directory
    @copy($image, "$folder/$image");

Once we get this working, we'll worry about adding the file path to the database.  Let me know how this turns out.
ASKER CERTIFIED SOLUTION
Avatar of cLFlaVA
cLFlaVA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lienny

ASKER

okay...so it's saving the path correctly in mysql, but it's still not saving the file into the directory.
how did it suddenly start saving the path?
are you getting any errors when trying to copy?
do you have proper permissions set for the uploads directory?
have you made the changes described above?
Avatar of lienny

ASKER

made change like you suggested above...then i put the $folder/$orig_filename into my insert statement...now when i submit the page it inserts the path.
no errors at all.
set the permissions to 777
Avatar of Roonaan
Reading the upload manual on php.net shows also that you never should use copy when handling uploaded files. cLFlaVA should be aware of the existence of move_uploaded_file() function.

This will not totaly solve your problem though, but could prevent problems when running on commercial server from webhosting parties.

Regards

-r-