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-25-2007, 05:52 PM   #1 (permalink)
The Visitor
 
Join Date: Oct 2007
Posts: 4
Thanks: 0
Shane is on a distinguished road
Default Uploading a Image

I have written a upload script and I ran into a small problem I didn't even think about. Say someone uploads logo.gif, if another person comes about and has a logo.gif they wanna upload then it already exsits. Is their anyway to add characters to the name while it is uploading so if logo.gif exsits and another person uploads that then it might generate logo1a12.gif or something? :D
Shane is offline  
Reply With Quote
Old 10-25-2007, 06:22 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
Wildhoney is on a distinguished road
Default

This function will count up until it finds an empty slot. Good if you want to keep your images in order, else just generate a random file name.

Basically:

Input image: myLogo.jpg
If myLogo.jpg exists change to myLogo.2.jpg
If myLogo.2.jpg doesn't exist, return myLogo.2.jpg

PHP Code:
define('DIRECTORY''./images/');

function 
getImageName($szImage)
{
    
$iIndex 2;
    
    if(!
file_exists(DIRECTORY $szImage))
    {
        return 
$szImage;
    }
    
    
$aParts explode('.'$szImage);
    
    if(
count($aParts) <= 1)
    {
        return 
$szImage;
    }
    
    
$szExt end($aParts);
    
$iCount count($aParts);
    
    
$aParts[$iCount 1] = $iIndex;
    
$aParts[$iCount] = $szExt;
    
    do { 
$aParts[count($aParts) - 2] = $iIndex++; }
    while(
file_exists(DIRECTORY implode('.'$aParts)));
    
    return 
implode('.'$aParts);

__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-25-2007, 08:01 PM   #3 (permalink)
The Visitor
 
Join Date: Oct 2007
Posts: 4
Thanks: 0
Shane is on a distinguished road
Default

I tried what you were saying. I might not be doing it right, but its just overwritting the ones that are already upload.

PHP Code:
<?php

$upload_dir 
"images/";   
$size_bytes 524880// = 5 megabytes(MB)
$limit_file_type "yes"
$allowed_file_type = array('image/gif',
                          
'image/pjpeg',
                          
'image/jpeg',
                          
'image/png',
                          
'image/jpg');

         
//check if the directory exist or not.
         
if (!is_dir("$upload_dir")) {
     die (
"The directory <b>($upload_dir)</b> doesn't exist");
         }
         
//check if the directory is writable.
         
if (!is_writeable("$upload_dir")){
            die (
"The directory <b>($upload_dir)</b> is NOT writable, Please Chmod (777)");
         }

//Check first if a file has been selected
//is_uploaded_file('filename') returns true if
//a file was uploaded via HTTP POST. Returns false otherwise.
if (is_uploaded_file($_FILES['filetoupload']['tmp_name']))
{
//begin of is_uploaded_file

        //Get the Size of the File
        
$size $_FILES['filetoupload']['size'];
        
//Make sure that $size is less than 1MB (1000000 bytes)
        
if ($size $size_bytes)
        {
            echo 
"File Too Large. File must be <b>$size_bytes</b> bytes.";
            exit();
        }
             
//check file type
        
if (($limit_file_type == "yes") && (!in_array($_FILES['filetoupload']['type'],$allowed_file_type)))
        {
            echo
"wrong file type";
            exit();
        }

        
// $filename will hold the value of the file name submetted from the form.
        
$filename =  $_FILES['filetoupload']['name'];
        
// Check if file is Already EXISTS.


define('DIRECTORY''./images/');

function 
getImageName($szImage)
{
    
$iIndex 2;
    
    if(!
file_exists(DIRECTORY $szImage))
    {
        return 
$szImage;
    }
    
    
$aParts explode('.'$szImage);
    
    if(
count($aParts) <= 1)
    {
        return 
$szImage;
    }
    
    
$szExt end($aParts);
    
$iCount count($aParts);
    
    
$aParts[$iCount 1] = $iIndex;
    
$aParts[$iCount] = $szExt;
    
    do { 
$aParts[count($aParts) - 2] = $iIndex++; }
    while(
file_exists(DIRECTORY implode('.'$aParts)));
    
    return 
implode('.'$aParts);



        
//Move the File to the Directory of your choice
        //move_uploaded_file('filename','destination') Moves afile to a new location.
        
if (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename)) {

           
//tell the user that the file has been uploaded and make him alink too;).
           
echo "File (<a href=$upload_dir$filename>$filename</a>) uploaded!";
           exit();

        }
        else
        {
            
//Print error
            
echo "There was a problem moving your file";
            exit();
        }
}
//end of is_uploaded_file


?>
Shane is offline  
Reply With Quote
Old 10-25-2007, 08:12 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
Wildhoney is on a distinguished road
Default

You need to get the image name from calling the function, like so:

PHP Code:
$filename =  getImageName($_FILES['filetoupload']['name']); 
Then just change:

PHP Code:
define('DIRECTORY''./images/'); 
To reflect the area where you're uploading the images to.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-25-2007, 09:04 PM   #5 (permalink)
The Visitor
 
Join Date: Oct 2007
Posts: 4
Thanks: 0
Shane is on a distinguished road
Default

Code:
define('DIRECTORY', 'images/');
function getImageName($szImage)
{
$filename =  getImageName($_FILES['filetoupload']['name']);
    $iIndex = 2;
    
    if(!file_exists(DIRECTORY . $szImage))
    {
        return $szImage;
    }
    
    $aParts = explode('.', $szImage);
    
    if(count($aParts) <= 1)
    {
        return $szImage;
    }
    
    $szExt = end($aParts);
    $iCount = count($aParts);
    
    $aParts[$iCount - 1] = $iIndex;
    $aParts[$iCount] = $szExt;
    
    do { $aParts[count($aParts) - 2] = $iIndex++; }
    while(file_exists(DIRECTORY . implode('.', $aParts)));
    
    return implode('.', $aParts);
}
Shane is offline  
Reply With Quote
Old 10-25-2007, 09:28 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
Wildhoney is on a distinguished road
Default

No offence intended, Shane, but I really think you need to read up on how functions work. Try this resource at PHP.net. Spend a good time reading through and learning it inside-out.

Essentially you need to call the function, this is done by placing $filename = getImageName($_FILES['filetoupload']['name']) outside of the function.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-25-2007, 11:01 PM   #7 (permalink)
The Visitor
 
Join Date: Oct 2007
Posts: 4
Thanks: 0
Shane is on a distinguished road
Default

For some reason I was thinking that calling it where I did would do what I was needing. I'm very novice when it comes to php and screw up way to much! I fixed the problem by reading the tutorial and now understand 100%! Appreciate all of your help wildhoney!
Shane 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 07:01 PM.

 
     

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