Jump to content

question


businessman332211@hotmail.com

Recommended Posts

// The code below I used from a friend named just some guy from the forums at // w3schools.  It takes the path name, and the// file name of the file I am using, and it tests the directory to see if the file// already exists.  If it exists then it takes the current filename and path// I am working with in my script, and renames the file, enought to where it can// still be added.  This isn't used necessarily to allow someone to upload// the same file 2 times, but more of if it ends up being the situation// where the filename is the same on 2 different computers and those files// just end up happening to have the same name.  // Just a safegaurd against discrepancies in case. // The whole section between comments was created by someone else.if (file_exists($basepath . $filename)) {  $nr = 0; $filename = $_FILES['lostimage']['name'] . "_{$nr}"; while (file_exists($basepath . $filename)) {   $filename = $_FILES['lostimage']['name'] . "_" . $nr++; }}// end script from just some guy on w3schools.com that tests if the filename// exists or not

obviously I got this from just some guy. The thing is I can't get it to work, it's only second priority, if I even get it working, it's a low budget project, that for now is only getting the essentials until they increase there budget later on down the road. I will be doing more when she get's income from the site, if the filename is named the same it keeps it the same, it's in a larger script. If the same file is uploaded it just saves it over the old one, which is fine, the picture can be shared by 2 posts, but if one post expires and delete's the picture then the other post is out of luck until it expires. This is something that may only happen one or 2 times within a year but it's still a possibility nevertheless. If I can't get this to work, I will just useif file_exists to test for it, and return an error if the file already exists.

Link to comment
Share on other sites

well you have to have $basepath and $filename already defined before you come to the script.$_FILES['lostimage']['name'] ... I don't know what this means. $_FILES['userfile']['name'] ... this means the filename it before it was uploaded. ---- ---you can check the time that the posts was posted and check that to the date on the image. Then if there within a day, delete the image. But if it's greater, then keep it. Does the date change when you upload a file, I forget... but if it does this will work.

Link to comment
Share on other sites

>_> I don't think you need a base path when checking to see if a file exists.

$nr = 0;$folder = 'images/';$fn = $_FILES['lostimage']['name']; //Why you have '_{$nr}' is oblivious to meif (file_exists($folder . $filename . "_" . $nr)){  while (file_exists($folder . $filename . $nr)) {   $nr++;  }  //move_uploaded_file($_FILES['lostimage']['tmp_name'], $basepath . $filename);}

Are you moving the uploaded file?

move_uploaded_file($_FILES['lostimage']['tmp_name'], $basepath . $filename);//move_uploaded_file(File temp name, path to new file (including filename))

The error I saw with your file_exists loop was that inside of it, $nr would be increased JUST for the name, and the variable would stay at 0.So if you increase $nr and check if the filename + _$nr exists, the variable will stay increased.

Link to comment
Share on other sites

$_FILES['lostimage']['name'] ... I don't know what this means. $_FILES['userfile']['name'] ... this means the filename it before it was uploaded.
The first element in the array is the name assigned to the HTML file element:<input type="file" name="lostimage">'userfile' only works if you named the file 'userfile'.$filename = $_FILES['lostimage']['name'] . "_" . $nr++;That line will append the number onto the end of the name, and increase the value of the counter for the next time it is needed. It will increase the value after it appends the current value.If it's not working, add some debugging statements to figure out what it is doing.
echo $basepath . $filename . "<br>";if (file_exists($basepath . $filename)) {   echo "file exists<br>";  $nr = 0;  $filename = $_FILES['lostimage']['name'] . "_{$nr}";  echo "filename: {$filename}<br>";    while (file_exists($basepath . $filename)) {	echo "file still exists<br>";	$filename = $_FILES['lostimage']['name'] . "_" . $nr++;	echo "new filename: {$filename}<br>";  }}else  echo "file was not found<br>";

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...