PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 23rd, 2013, 12:34 PM
LukePVB LukePVB is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 LukePVB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 57 sec
Reputation Power: 0
Help with uploading images

Hi, I have a script that sends uploaded images to my images folder on my web server. Im not sure but I think it also renames the images (which is what I want to happen). Could someone please take a look at this script for me and tell me if it does rename the images?

I also want the script to send the name of the image, after it's renamed, to the database table in row labeled 'images'. Could someone help me with this?

Heres the script:
PHP Code:
<?php
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE'1024 50);
if (
array_key_exists('upload'$_POST)) {
  
// define constant for upload folder
  
define('UPLOAD_DIR''/path/to/images/');
  
// replace any spaces in original filename with underscores
  
$file str_replace(' ''_'$_FILES['image']['name']);
  
// create an array of permitted MIME types
  
$permitted = array('image/gif''image/jpeg''image/pjpeg',
'image/png');
  
// upload if file is OK
  
if (in_array($_FILES['image']['type'], $permitted)
          && 
$_FILES['image']['size'] > 
          
&& $_FILES['image']['size'] <= MAX_FILE_SIZE) {
        switch(
$_FILES['image']['error']) {
          case 
0:
                
// check if a file of the same name has been uploaded
                
if (!file_exists(UPLOAD_DIR $file)) {
                  
// move the file to the upload folder and rename it
                  
$success =
move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR .
$file);
                } else {
                  
$result 'A file of the same name already exists.';
                }
                if (
$success) {
                  
$result "$file uploaded successfully.";
                } else {
                  
$result "Error uploading $file. Please try again.";
                }
                break;
          case 
3:
          case 
6:
          case 
7:
          case 
8:
                
$result "Error uploading $file. Please try again.";
                break;
          case 
4:
                
$result "You didn't select a file to be uploaded.";
        }
  } else {
        
$result "$file is either too big or not an image.";
  }
}
?>

Reply With Quote
  #2  
Old February 24th, 2013, 12:32 AM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,947 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 10 h 37 m 24 sec
Reputation Power: 7053
Your upload script is not secure at all because you do not validate the file extension.

It may or may not rename the file, depending on the file's original name, but if you want it to rename the file then presumably you already have some scheme in mind for renaming the file?
__________________
PHP FAQ
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #3  
Old February 24th, 2013, 02:21 AM
Nanomech's Avatar
Nanomech Nanomech is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: The Pleiades
Posts: 196 Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 23 h 53 m 4 sec
Reputation Power: 7
Send a message via Skype to Nanomech
Validate the file extension first, there are a number of ways to do this in PHP.

Next, just run a custom function something like edit_filename(){}

You do not need to pass in the value of your file name because superglobals are available to the whole script including functions, without the need to pass the value in via a parameter.

There are some handy string functions which allow you to cut the extension off the filename string, store it somewhere, you can then generate a random 2 digit number for example, concatenate that to the end of the filename, then concatenate the extension value back onto the filename to give you the 'edited' filename. I've currently wrote something exactly like this for my current site and it works good.

Kind regards,

NM.
__________________
"WERE NOT WORTHY!"
"WERE NOT WORTHY!"

Reply With Quote
  #4  
Old February 24th, 2013, 08:29 AM
engrmudasir engrmudasir is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Location: Pakistan
Posts: 3 engrmudasir User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 39 sec
Reputation Power: 0
Send a message via Skype to engrmudasir
Facebook
Quote:
Originally Posted by LukePVB
Hi, I have a script that sends uploaded images to my images folder on my web server. Im not sure but I think it also renames the images (which is what I want to happen). Could someone please take a look at this script for me and tell me if it does rename the images?

I also want the script to send the name of the image, after it's renamed, to the database table in row labeled 'images'. Could someone help me with this?

Heres the script:
PHP Code:
<?php
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE'1024 50);
if (
array_key_exists('upload'$_POST)) {
  
// define constant for upload folder
  
define('UPLOAD_DIR''/path/to/images/');
  
// replace any spaces in original filename with underscores
  
$file str_replace(' ''_'$_FILES['image']['name']);
  
// create an array of permitted MIME types
  
$permitted = array('image/gif''image/jpeg''image/pjpeg',
'image/png');
  
// upload if file is OK
  
if (in_array($_FILES['image']['type'], $permitted)
          && 
$_FILES['image']['size'] > 
          
&& $_FILES['image']['size'] <= MAX_FILE_SIZE) {
        switch(
$_FILES['image']['error']) {
          case 
0:
                
// check if a file of the same name has been uploaded
                
if (!file_exists(UPLOAD_DIR $file)) {
                  
// move the file to the upload folder and rename it
                  
$success =
move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR .
$file);
                } else {
                  
$result 'A file of the same name already exists.';
                }
                if (
$success) {
                  
$result "$file uploaded successfully.";
                } else {
                  
$result "Error uploading $file. Please try again.";
                }
                break;
          case 
3:
          case 
6:
          case 
7:
          case 
8:
                
$result "Error uploading $file. Please try again.";
                break;
          case 
4:
                
$result "You didn't select a file to be uploaded.";
        }
  } else {
        
$result "$file is either too big or not an image.";
  }
}
?>

Just use Two Variables With $OriginalName and $NewName, and just put this in your original name variable as $originalName = $_FILES['FileField']['name']; and Just right after this you need to change this with rename() function available in PHP. and save that with your $newName and then you can insert both values into your database as INSERT INTO tablename (OriginalName, NewName) VALUES ($originalName, $newName);
And in the same case you will upload the file into your directory with new name move_uploaded_file($newName and Your destination folder here);

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Help with uploading images

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap