?
Solved

PHP mySQL Upload, Rename and Relocate Image File

Posted on 2006-06-30
11
Medium Priority
?
282 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
[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
  • 3
  • 2
  • 2
  • +1
11 Comments
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 200 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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 

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 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

800 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