/    Sign up×
Community /Pin to ProfileBookmark

I need help creating an unique file name

[code=php]$temp_image_name = $_FILES[ $field ][ ‘name’ ];
$num = 1;
while (1) {
if (file_exists(“http://linksku.com/wp-content/upload/” . $temp_image_name)) {
if ($num == 1) {
preg_match(‘/.[a-zA-Z]{3,4}$/’,$temp_image_name,$match);
$extension = $match[0];
}
$temp_image_name = preg_replace(‘/.[a-zA-Z]{3,4}$/’,”,$temp_image_name);
if ($num > 1)
$regex = ‘/’.$num.’$/’;
if ($num > 1)
$temp_image_name = preg_replace($regex,”,$temp_image_name);
$num = $num + 1;
$temp_image_name = $temp_image_name.$num;
} else {
if ($num!=1)
$temp_image_name = $temp_image_name.$extension;
break;
}
}[/code]

I want this to create an unique file name, but this isn’t working. I know there are much more efficient ways of writing this.

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@narutodude000authorJul 21.2010 — P.S. The filenames are incrementing.
Copy linkTweet thisAlerts:
@Shanu_chaudharyJul 21.2010 — Try this...

[code=php]
$temp_image_name = $_FILES[ $field ][ 'name' ];
$num = 1;
while ($num) {
if (file_exists("http://linksku.com/wp-content/upload/" . $temp_image_name)) {
if ($num == 1) {
preg_match('/.[a-zA-Z]{3,4}$/',$temp_image_name,$match);
$extension = $match[0];
}
$temp_image_name = preg_replace('/.[a-zA-Z]{3,4}$/','',$temp_image_name);
if ($num > 1)
$regex = '/'.$num.'$/';
if ($num > 1)
$temp_image_name = preg_replace($regex,'',$temp_image_name);
$num = $num + 1;
$temp_image_name = $temp_image_name.$num;
} else {
if ($num!=1)
$temp_image_name = $temp_image_name.$extension;
break;
}
$num++;
}
[/code]


Hope this helps..
Copy linkTweet thisAlerts:
@MindzaiJul 21.2010 — Or you could use uniqid.
Copy linkTweet thisAlerts:
@narutodude000authorJul 21.2010 — [code=php]$num = 2;
while (1) {
if (file_exists("../wp-content/uploads/wp-post-image/" . $temp_image_name)) {
if ($num == 2) {
preg_match('/.[a-zA-Z]{3,4}$/',$temp_image_name,$match);
$extension = $match[0];
}
$temp_image_name = preg_replace('/.[a-zA-Z]{3,4}$/','',$temp_image_name);
if ($num > 2)
$regex = '/'.$num.'$/';
if ($num > 2)
$temp_image_name = preg_replace($regex,'',$temp_image_name);
$temp_image_name = $temp_image_name.$num;
} else {
if ($num > 2)
$temp_image_name = $temp_image_name.$extension;
break;
}
$num++;
} [/code]

I fixed it by making the path relative, since file_exists() returns false when the path is absolute and safe mode is on.
Copy linkTweet thisAlerts:
@Jarrod1937Jul 21.2010 — You really need to learn and understand the difference between a url and a system absolute path. For one, it is quite possible it doesn't allow remote access to a file via url in safe mode... but also quite possible that it does for local files, while in safe mode.

When you request a file via url, you're telling php that the file is remote, and so every time it will have to do a dns lookup for the domain, request the file, get a response, all of which is running throughout the network known as the internet. Compare this to providing the function, file_exists(), with a system absolute path where it just has to make a system call to the file system and get the results. For a test/procedure on any local file you want to use a local absolute (or relative if it fits the situation) path to it, as it is many times faster than remote access via url, not to mention many times procedures on remote files have limited permissions/access for security reasons.
Copy linkTweet thisAlerts:
@narutodude000authorJul 21.2010 — So I can use /public_html/forum/styles/prosilver/template/overall_header.html in a PHP include?
Copy linkTweet thisAlerts:
@MindzaiJul 21.2010 — If that file really exists on the server (which it wont!). There is a difference between urls and filesystem paths as mentioned above. Include expects a filesystem path.
Copy linkTweet thisAlerts:
@Jarrod1937Jul 21.2010 — So I can use /public_html/forum/styles/prosilver/template/overall_header.html in a PHP include?[/QUOTE]
Yes, that would correspond to:

http://linksku.com/forum/styles/prosilver/template/overall_header.html

Though normally, the entire absolute path is something like:

/home/username(whateverthismaybe)/public_html/forum/styles/prosilver/template/overall_header.html (i'm assuming a linux/unix type server).

An absolute file system path is a combo of two things, the document root path and the path to the file from that root. In the above example, the /home/username/public_html/ is the doc root. To get the complete path of the document root you can generally echo out the value of $_SERVER['DOCUMENT_ROOT'], or use it to make an absolute path. Though, as stated previously, a relative path is just fine too, the main thing is just to understand is that using a url makes php treat it as a remote file (and if you try to use a url for a php file you'll only get the output of the php file code, not the code of the file itself). Using a url for a file operation will still work for items like images, like you were trying to do earlier, but by using a url you have to go through many different systems to get the final result. This could include many servers and firewalls, all to get the same result that a local file system request would get in a near instant.

However, since a php include requires a php file, it wouldn't work for that particular .html file you're requesting in the above code. For that html file you'd need to use something like file_get_contents() or file().
Copy linkTweet thisAlerts:
@criterion9Jul 22.2010 — 
Yes, that would correspond to:

http://linksku.com/forum/styles/pros...ll_header.html
[/quote]

Not true in most cases. In PHP, the include statement takes either a relative path or an absolute one. The example provided used an absolute path (on *nix systems) that (more than likely) does not exist. A "/" at the beginning (on a *nix system) makes PHP start at the root of the file system.
Copy linkTweet thisAlerts:
@narutodude000authorJul 22.2010 — However, since a php include requires a php file, it wouldn't work for that particular .html file you're requesting in the above code. For that html file you'd need to use something like file_get_contents() or file().[/QUOTE]
My bad, I was editing that file when I used it as an example, and I forgot that it wasn't PHP :o. Thanks for your explanation, the only part where I'm still confused is, how does the PHP processor distinguish the difference between a relative URL and a relative filesystem path? They're both written the same way, right (correct me from wrong).
Copy linkTweet thisAlerts:
@criterion9Jul 22.2010 — 
However, since a php include requires a php file, it wouldn't work for that particular .html file you're requesting in the above code. For that html file you'd need to use something like file_get_contents() or file().
[/quote]

This is also not true. file_get_contents() or file() will read the contents of a file to a variable. include will (as on php.net):

The include() statement includes and evaluates the specified file.
[/quote]
Copy linkTweet thisAlerts:
@Jarrod1937Jul 22.2010 — This is also not true. file_get_contents() or file() will read the contents of a file to a variable. include will (as on php.net):[/QUOTE]
Yes, sorry you're correct, i was thinking of the include causing the include text to be parsed, but you can include any file and it will be included just like any other server side type of include.
Copy linkTweet thisAlerts:
@Jarrod1937Jul 22.2010 — My bad, I was editing that file when I used it as an example, and I forgot that it wasn't PHP :o. Thanks for your explanation, the only part where I'm still confused is, how does the PHP processor distinguish the difference between a relative URL and a relative filesystem path? They're both written the same way, right (correct me from wrong).[/QUOTE]
Not sure what you mean by relative url in this case. A web url is treated as that, a web url, so it treats any file located by a url as a remote file. However, a file system path (relative or absolute) does not begin with a protocol specification like http, and treats any file referenced as such as local. The difference between relative and absolute file system paths is less important, both work perfectly fine for just about any situation. However, it may be preferred to an absolute paths for certain operations.
Copy linkTweet thisAlerts:
@Jarrod1937Jul 22.2010 — Not true in most cases. In PHP, the include statement takes either a relative path or an absolute one. The example provided used an absolute path (on *nix systems) that (more than likely) does not exist. A "/" at the beginning (on a *nix system) makes PHP start at the root of the file system.[/QUOTE]
Yes, sorry if that was not clear, that is why i proceeded to say "Though [B]normally[/B], the entire absolute path is something like:

/home/username(whateverthismaybe)/public_html/forum/styles/prosilver/template/overall_header.html", it was sort of my way of indirectly correcting what he said. It indeed would be very unlikely he'd have a file path starting from root of /public_html/...etc.
Copy linkTweet thisAlerts:
@MindzaiJul 22.2010 — My bad, I was editing that file when I used it as an example, and I forgot that it wasn't PHP :o. Thanks for your explanation, the only part where I'm still confused is, how does the PHP processor distinguish the difference between a relative URL and a relative filesystem path? They're both written the same way, right (correct me from wrong).[/QUOTE]

It's often about context, in that a function may only take either a path or url. Other times (including in the case of include), the string itself will be examined. Bear in mind that things like:

/public_html/foo/bar/etc

are *never* URLs in the true sense, although often you will see them refered to as "relative URLs". URLs specify where a resource is located (which this does), and [I]how the resource should be retrieved[/I] (which this does not). This second part is handled by specifying the relevant protocol as part of the URL, for example http://.

If you wanted to include a URL (assuming your PHP config allows it), you would need to actually specify a real URL. Anything else will be treated as a local filesystem path. Also bear in mind that including a URL will actually include the [I]output[/I] of any script at that URL, which will of course not include any PHP code.
Copy linkTweet thisAlerts:
@narutodude000authorJul 22.2010 — Thanks. What confused me was that some articles online called "../../file.php" an relative URL.

Another question is, if I include a file containing functions by its URL, can I still access the functions?
Copy linkTweet thisAlerts:
@MindzaiJul 22.2010 — Thanks. What confused me was that some articles online called "../../file.php" an relative URL.

Another question is, if I include a file containing functions by its URL, can I still access the functions?[/QUOTE]


No. All you will get is the output of the script, which in a file containing only function definitions would be nothing.
Copy linkTweet thisAlerts:
@narutodude000authorJul 22.2010 — I just realized earlier that my script didn't work properly. It stopped incrementing after the second image. For example, these were the files that were uploaded:
[CODE]/public_html/wp-content/uploads/wp-post-thumbnail/Balloon-Explode.jpg
/public_html/wp-content/uploads/wp-post-thumbnail/Balloon-Explode2.jpg
/public_html/wp-content/uploads/wp-post-thumbnail/Balloon-Explode2.jpg
/public_html/wp-content/uploads/wp-post-thumbnail/Balloon-Explode2.jpg[/CODE]

The older file was replaced by the newer file with the same name.

The function is here:
[code=php]$num = 2;
while (1) {
if (file_exists("../wp-content/uploads/wp-post-image/" . $temp_image_name)) {
if ($num == 2) {
preg_match('/.[a-zA-Z]{3,4}$/',$temp_image_name,$match);
$extension = $match[0];
}
$temp_image_name = preg_replace('/.[a-zA-Z]{3,4}$/','',$temp_image_name);
if ($num > 2)
$regex = '/'.$num.'$/';
if ($num > 2)
$temp_image_name = preg_replace($regex,'',$temp_image_name);
$temp_image_name = $temp_image_name.$num;
} else {
if ($num > 2)
$temp_image_name = $temp_image_name.$extension;
break;
}
$num++;
}[/code]
×

Success!

Help @narutodude000 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...