PDA

View Full Version : Help with...



Jeffreyv1987
09-04-2009, 06:36 AM
Alright im having trouble with something...
What im looking for is something so if somone uploads an image and another person uploads another image with the same name say 'image.gif' id want the other image to be renamed 'image1.gif' how would i do this?? i found this



<?php
$img = "images/".$_FILES['bilde']['name'];
$t=0;
while(file_exists($img)){
$img = "images/".$_FILES['bilde']['name'];
$img=substr($img,0,strpos($img,"."))."_$t".strstr($img,".");
$t++;
}
move_uploaded_file($_FILES['bilde']['tmp_name'], $img);
?>


I'm having trouble with it... would somone be willing to help me out?

Im useing it for a dynamic signature script i've been working on and finally got it working today. but it over writes the image name when i put in the same name, and if i let my visitors use it and some how they use the same name i wouldnt want it to mess up there signature with somone elses.

JasonDFR
09-04-2009, 08:30 AM
What trouble are you having?

forum_amnesiac
09-04-2009, 02:14 PM
Haven't tested it but I think your problem is how you're treating $t.

try this



$img = "images/".$_FILES['bilde']['name'];
$t=0;
while(file_exists($img)){
$img = "images/".$_FILES['bilde']['name'];
$img=substr($img,0,strpos($img,".")).$t.".".strstr($img,".");
$t++;
}
move_uploaded_file($_FILES['bilde']['tmp_name'], $img);

Jeffreyv1987
09-04-2009, 03:04 PM
i know i sound kinda dumb right now but im like confused right now lol

would 'bilde' be the file name? how would i make it so my file would work in here..

JasonDFR
09-04-2009, 04:05 PM
bilde is the name of the file input in your form:

<input name="bilde" type="file" />

$_FILES['bilde']['name'] is the name of the file as it is on the user's computer.
$_FILES['bilde']['tmp_name'] is the name(path) of the file on the server running PHP (probably your web server).

Jeffreyv1987
09-04-2009, 08:06 PM
Oh alright.. sorry lol just now learning alittle bit of PHP..

so ive got a form that looks like the one below on my site right now.


<form action="" method=POST>

<input type="text" name="id" value="img">

<input type="submit" value="Submit">
</form>


What it does once its been submitted is grabs the 'imagecreatefrompng' ive got and makes it into 'img.png' and then sends it to the directory 'images' how would i make it work for something like this?

JasonDFR
09-05-2009, 05:45 AM
http://www.tizag.com/

Go through some tutorials first. HTML and PHP.

Good luck.

Jeffreyv1987
09-05-2009, 06:47 PM
Alright cool..

Right now i just put a thing to check if the image exists and it tells the users that theres a image with the same name.

but not sure what to do to make it the way i want..

JShor
09-05-2009, 07:48 PM
Well, you should first be aware that if a file with the same name exists, the old one would be overwritten. Now when you say "image exists", do you mean that they inputted an image?

THis so far will check if image exists, and if user inputted image:


<?php

$img = "images/".$_FILES['bilde']['name'];
$t=0;
while(file_exists($img)){
$img = "images/".$_FILES['bilde']['name'];
$img=substr($img,0,strpos($img,".")).$t.".".strstr($img,".");
$t++;
}

if(isset($_POST['blide'])) { if user inputted an image
if(file_exists($_FILES['blide']['name'])) { // if file exists already
echo "This file already exists in this directory.";
} else { // all req. met, upload file
move_uploaded_file($_FILES['bilde']['tmp_name'], $img);
echo "File successfully uploaded.";
}
} else { // if user did NOT input an image
echo "You must input an image first!";
}

?>


and your form should look like:


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
File: <input type="file" name="blide" />
<input type="submit" value=" Upload " />
</form>


&& like Jason said, you should probably go thru PHP and HTML tutorials first.

HTH:)

Jeffreyv1987
09-06-2009, 07:04 AM
Alright ive got it working the way i want it thanks for helping people.

JShor
09-06-2009, 07:10 AM
Good to hear :)