check image directory limit 1 image upload..need help please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

check image directory limit 1 image upload..need help please

Post by scarface222 »

Hey guys I just have an image upload script that checks the image then uploads it to a directory. I just want to make it so that in the upload process it checks the directory and if an image exists it deletes and replaces. I am not sure how to approach the checking of the directory. Anyone have any ideas? Help greatly appreciated.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: check image directory limit 1 image upload..need help please

Post by jackpf »

I think move_uploaded_file() will replace any existing file anyway.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: check image directory limit 1 image upload..need help please

Post by scarface222 »

this is my script which contains it already and the file is not replaced. I think need some sort of function to open directory and check if there is an image present or file but I am not sure exactly how to approach this. Currently images are just added to the directory and do not overwrite each other for obvious reasons.

Code: Select all

 
<?php
 
if (isset($_POST['upload']))
{
 
# edit #
    $maxwidth = 1024;
    $maxheight = 1024;
    $max_filesize = 2024000;
$username=$session->username;
    $uploads = "../usercontent/$username/images";
    $types_array = array('image/gif','image/jpeg','image/x-png', 'image/jpg');
# end edit #
 
if($_FILES['image']['name'] == "")
{
    echo "Please select a file to upload!\n";
    return;
}
 
if(!in_array($_FILES['image']['type'], $types_array))
{
    echo "That file type is not allowed!\n";
    return;
}
 
    $max_filesize_kb = ($max_filesize / 1024);
 
if($_FILES['image']['size'] > $max_filesize)
{
    echo "Your file is too large, files may be up to ".$max_filesize_kb."kb\n";
    return;
}
 
    $imagesize = getimagesize($_FILES['image']['tmp_name']);
 
    $imagewidth = $imagesize[0];
    $imageheight = $imagesize[1];
 
if($imagewidth > $maxwidth || $imageheight > $maxheight)
{
    echo "Your file is too large, files may be up to ".$maxwidth."px x ".$maxheight."px in size\n";
    return;
}
move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.urlencode($_FILES['image']['name']))
or die ("Couldn't upload ".$_FILES['image']['name']."\n");
 
echo 'file uploaded';
 
 
}
?>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: check image directory limit 1 image upload..need help please

Post by jackpf »

Well, if you read the notes on move_uploaded_file(), you'll see a big red box saying something like "Warning: existing files will be replaced" or something, so I'm not sure how that's not the case for you...
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: check image directory limit 1 image upload..need help please

Post by scarface222 »

yes if the file already exists but I want it so people can change the picture if they want so they won't be uploading the same file. I want to be able to check the directory and no matter what delete the current file and replace it.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: check image directory limit 1 image upload..need help please

Post by jackpf »

In that case you'll have to record who uploads what in the database.

Then, when someone uploads something, run a query to check if they've already uploaded something. If they have, then delete the existing file, update the record to the new file, and upload the new file.

Or you could just call the file the user's username, or IP address, or however you're keeping track of who's uploading what. That means that the next time they go to upload something, the file will have the same name, so will overwrite the old one.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: check image directory limit 1 image upload..need help please

Post by scarface222 »

no way to just check directory eh? Alright thanks for the help man I appreciate it.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: check image directory limit 1 image upload..need help please

Post by jackpf »

Well, you'd need to know what file to check the directory for.

So, you'll need to know what the name of the previous file they uploaded was, which is why you'd have to keep records of who uploaded what in the database.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: check image directory limit 1 image upload..need help please

Post by scarface222 »

k thanks man, just curious can move_uploaded_file be used to copy and paste a file from another directory to the folder you want. I want to have a default file put in the folder when it is created.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: check image directory limit 1 image upload..need help please

Post by jackpf »

Wait...you only want one image in directory?

That can be done...
Like this:

Code: Select all

<?php
    $dir = glob('directory/*.*');
    
    if(count($dir) > 0)
        //there is a file
    else
        //there isn't a file
?>
And no, you'll have to use fopen() for that.
Post Reply