file upload and save path to mysql

Posted on 2004-10-19
Last Modified: 2013-12-12

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:

$folder = "/recipes/uploads/";
  @copy("$image" , "$folder/$image_name");
      $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')";

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

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 13

Expert Comment

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.

Author Comment

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

Expert Comment

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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 13

Accepted Solution

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");

Author Comment

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

Expert Comment

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?

Author Comment

ID: 12351066
made change like you suggested above...then i put the $folder/$orig_filename into my insert when i submit the page it inserts the path.
no errors at all.
set the permissions to 777
LVL 49

Expert Comment

ID: 12361292
Reading the upload manual on 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.



Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add a date range function to PDF Generation 9 42
Code planning methods/tools? 5 56
How do I correctly structure this insert statement 3 32
Phone number mask 4 14
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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 ( and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 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