TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-17-2007, 08:51 PM   #1 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default Renaming Uploaded Files

Hey,

Have made a script that basically uploads a file... nothing special. I wanted to rename each file with the user's username, so if my username is Test User the file would rename to test_user.ext.

When I echo out the username and file name, they appear correct, but when I upload the image, it renames it to Array.ext. Not sure why it's outputting as an array, when I echo it out normally, it shows.

Here's some code:

PHP Code:
<?php
// Rename file according to username
foreach(getUser($_COOKIE['userID']) as $info) {
    
$ext explode('.'$_FILES['upload_avatar']['name']);
    
$file_name str_replace($ext[0], strtolower($info['username']), $ext);
                
    
// Avatar directory
    
$directory 'images/avatar/'.$file_name.'.'.$ext[1];
    
// Move file to directory
    
move_uploaded_file($_FILES['upload_avatar']['tmp_name'], $directory);
}
?>
Can anyone see anything that I might have missed? Can't work my head around it :confused:

Thanks
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 10-17-2007, 10:12 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

PHP Code:
// Old, wrong
$file_name str_replace($ext[0], strtolower($info['username']), $ext);

// New, slightly better
$file_name str_replace($ext[0], strtolower($info['username']), $_FILES['upload_avatar']['name']); 
You were trying to do a string replacement (str_replace()) on an array ($ext). If you read the PHP manual page, you'd know that if an array is used then the return value is also an array. Because of that, when you try to put together the $directory variable, PHP converts the $file_name array into a string: "Array". That's why your files were being called Array.ext.
Salathe is offline  
Reply With Quote
Old 10-18-2007, 02:12 AM   #3 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Wiked!

Thank you Salathe :)
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:13 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design