Solved

PHP mySQL Upload, Rename and Relocate Image File

Posted on 2006-06-30
11
263 Views
Last Modified: 2013-12-12
Ok, I'm making a real estate site and I'm currently making the upload form. I need code that will let the user upload an image, then the system will automatically rename it (based on the auto inc id or whatever you think is better and place it in a specific folder). I've heard images can be stored as longblobs but I'd rather not do that and keep it the way it is. Can anyone help?
0
Comment
Question by:kornflake
  • 3
  • 2
  • 2
  • +1
11 Comments
 
LVL 40

Accepted Solution

by:
RQuadling earned 50 total points
ID: 17017923
It is far better to store the path to the image in the DB.

Image I uploaded the 25MB image of the Orion Nebula from NASA (it looks REALLY nice).

Then you stored that in the db. To get it to the client you would need to get it from the DB before you could send it the client.

By using a file store, you can use the ftp command fpassthru or readfile and not use any more memory. Serving files to users is what webservers are good for. Storing and indexing data is what DB servers are for.
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 17018183
RQuadling has a very strong point here, you'd be asking your db server too much it store everything there even binary files. save image path only in the db. but be careful when you manipulate them later, like deleting image, renaming it, etc... you may just end with some a synchronus files with irrelevant paths store in db. i like to do these stuff oo,
this way, when i use $image->rename (oldname,newname)
i put in that method both renaming the physical image and the path in the db !
0
 
LVL 1

Expert Comment

by:dimakh
ID: 17041116
Hi Kornflake,

here is a dummy script im provinding as per your requirements. please change fieldsnames or file paths as per your scripts while using it.



<?
/************************************************************
 variables definations:

$new_file_name = newly generated file name by appending datetime stamp;
$dst_file_name   = the physical path wher the new file will be saved.
$server_location = the physical directory path where file is to be stored.

************************************************************/

      if($_GET['mode'] == 'send'){

            $file_temparray            = split('\.',$_FILES['file_name']['name']);
            
            $new_file_name      =  $file_temparray[0].'_'.date('YmdHis').'.'.$file_temparray[1];
            $dst_file_name      =  $server_location. $new_file_name ;
            $tmpfile                  =  $_FILES['report_file']['tmp_name'];

            $copy_status =       @copy ( $tmpfile , $dst_file_name) ;

      }            
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form name="frmgml" action="<?=$PHP_SELF?>?mode=send" method="post" enctype="multipart/form-data">
<table width="530" height="" border="0" cellpadding="0" cellspacing="0">
  <tr>
      <td>Browse a File : </td>
      <td><input type="file" name="file_name" size="25"> </td>
  </tr>
  <tr>
      <td colspan="4" height="20">
      <input type="submit" name="upload_file" value="Submit">
      </td>
  </tr>
</table>
</form>

</body>
</html>


hope this helps you. if any queries, please let me know.

Thanks.
0
 

Author Comment

by:kornflake
ID: 17041220
  if($_GET['mode'] == 'send'){

          $file_temparray          = split('\.',$_FILES['file_name']['name']);
         
          $new_file_name     =  $file_temparray[0].'_'.date('YmdHis').'.'.$file_temparray[1];
          $dst_file_name     =  $server_location. $new_file_name ;
          $tmpfile               =  $_FILES['report_file']['tmp_name'];

          $copy_status =      @copy ( $tmpfile , $dst_file_name) ;

     }  

ok, this looks good, a few questions though.......how and where do i define the Image location - something like $server_location = ImageFolderName or what?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Expert Comment

by:dimakh
ID: 17042266
hi,

you should define Image location as:
if you image direcory path is http://www.yoursitename.com/images then the variable value should be:

$server_location = $_SERVER['DOCUMENT_ROOT']."/images/";

you can define this variable in the same file or in anyother include file, which you are calling in your script.

lemme know if you've anyother queries.


Thanks.
0
 

Author Comment

by:kornflake
ID: 17064559
    if($_GET['mode'] == 'send'){

          $file_temparray    =  split('\.',$_FILES['file_name']['name']);
          $new_file_name     =  $file_temparray[0].'_'.date('YmdHis').'.'.$file_temparray[1];
          $dst_file_name     =  $server_location. $new_file_name ;
          $tmpfile           =  $_FILES['report_file']['tmp_name'];
              $server_location   =  $_SERVER['DOCUMENT_ROOT']."/Images/";
          $copy_status =        @copy ( $tmpfile , $dst_file_name) ;

     }  

Its still not working...is there anyway I can find out why? like debugging messages?
0
 
LVL 1

Assisted Solution

by:dimakh
dimakh earned 50 total points
ID: 17070771
sorry there was a small mistake in my code. $tmpfile value was wrong.
here is updated code:

if($_GET['mode'] == 'send'){

          $file_temparray    =  split('\.',$_FILES['file_name']['name']);
          $new_file_name     =  $file_temparray[0].'_'.date('YmdHis').'.'.$file_temparray[1];
          $dst_file_name     =  $server_location. $new_file_name ;
          $tmpfile           =  $_FILES['file_name']['tmp_name'];
          $server_location   =  $_SERVER['DOCUMENT_ROOT']."/Images/";
          $copy_status =        @copy ( $tmpfile , $dst_file_name) ;
}  

if still your problem persists, please debug it step by step... for ex:
1. check whether file is getting uploaded or not... you can check it by printing $_FILES['file_name']['tmp_name'];
2. then check whether the destination path where the file is ot be copied is correct. print $dst_file_name
3. finally remove @ the copy .. and check if you are getting any error messages.

Thanks.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 17752618
Split
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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…
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…
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.

758 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

19 Experts available now in Live!

Get 1:1 Help Now