The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Help with uploading images
Discuss Help with uploading images in the PHP Development forum on Dev Shed. Help with uploading images PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 23rd, 2013, 12:34 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 1
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'] > 0
&& $_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.";
}
}
?>
|

February 24th, 2013, 12:32 AM
|
 |
Lost in code
|
|
|
|
|
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?
|

February 24th, 2013, 02:21 AM
|
 |
Contributing User
|
|
Join Date: Aug 2011
Location: The Pleiades
|
|
|
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!"
|

February 24th, 2013, 08:29 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Location: Pakistan
Posts: 3
Time spent in forums: 43 m 39 sec
Reputation Power: 0
|
|
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'] > 0
&& $_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);
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|