Solved

PHP mySQL Upload, Rename and Relocate Image File

Posted on 2006-06-30
11
266 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
change password links 7 74
Amazon Redshift 2 26
How do I put a session for a radio button? 4 13
Session timeout 5 14
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 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…

920 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