Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

file upload and save path to mysql

Posted on 2004-10-19
8
Medium Priority
?
288 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
[X]
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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Accepted Solution

by:
cLFlaVA earned 375 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
 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

670 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