Solved

PHP mySQL Upload, Rename and Relocate Image File

Posted on 2006-06-30
11
270 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:
Richard Quadling 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 
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:Richard Quadling
ID: 17752618
Split
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

773 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