Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2011, 05:15 AM   PM User | #1
cjason
New to the CF scene

 
Join Date: Oct 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
cjason is an unknown quantity at this point
Renaming certain files after upload.

Hi all, i've got a script that i've part edited to accept only certain file types to be uploaded, eg; JPG, PNG, JPEG etc. the script is originally part of the "uploadify script", im new to php so im finding it hard to work out how i can add a function to rename files on the backend so i dont get duplicates or errors on upload, but keep original names for the frontend. if this all makes sense, thanks in advance - Cody

Code:
<?php

if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
	$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
	
	
	$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
	$fileParts = pathinfo($_FILES['Filedata']['name']);
	
	if (in_array($fileParts['extension'],$fileTypes)) {
		move_uploaded_file($tempFile,$targetFile);
		echo '1';
	} else {
		echo 'Invalid file type.';
	}
}
?>
cjason is offline   Reply With Quote
Old 10-08-2011, 05:44 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
No that doesn't really make any sense. What is the difference between the front and back end when it comes to this code? The user can upload any file they want, you can rename it to anything you want.
To rename, simply modify $targetFile to change what you need. The problem I see overall is on conflict even with a conflict detection and correction, how will you determine which of the conflicted files is the same as this file just uploaded at a later time?
You'd be better off using a database to associate random generated file names to the user uploading them (or whatever you plan on using for an association). Then you can display whatever you want within the database and the file names themselves have no impact on the display.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:21 PM.


Advertisement
Log in to turn off these ads.