Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP mySQL Upload, Rename and Relocate Image File

Posted on 2006-06-30
11
Medium Priority
?
285 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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 …
Suggested Courses

610 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