Solved

file upload and save path to mysql

Posted on 2004-10-19
8
274 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now