Solved

file upload and save path to mysql

Posted on 2004-10-19
8
275 Views
Last Modified: 2013-12-12
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>
0
Comment
Question by:lienny
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12349455
well, the path would be

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

Author Comment

by:lienny
ID: 12349852
that still doesn't resolve the path name being stored in the db.  also, it doesn't save my file to the uploads directory.
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12350175

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.
0
 
LVL 13

Accepted Solution

by:
cLFlaVA earned 125 total points
ID: 12350225
Actually, do it like this:

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

    # get the original filename so it has the same name when saved
    $orig_filename = $_FILES['image']['name'];

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

    # copy the temp file to the file directory
    @copy($image, "$folder/$orig_filename");
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:lienny
ID: 12350752
okay...so it's saving the path correctly in mysql, but it's still not saving the file into the directory.
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12350942
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?
0
 

Author Comment

by:lienny
ID: 12351066
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
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12361292
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-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now