- This topic is empty.
August 11, 2011 at 9:51 am #33877
I have a simple image upload form, it uploads the photo and uses a move_uploaded_file function to move it from a temporary directory to a directory on my server, the problem with this is that all the images have the same name (image.jpg) and kept over writing each other, this is the code I have:
after that i added in:
$random = substr(md5(rand(0,9999)), 4, 10);
that I pass the file through a rename where I name it image.$random.jpg, so how would I now go about getting the actual url for this, I.E. example.com/images/image.dji32.jpg
and what code would I use to achieve that?
and FINALLY, after I’ve got that url, how could I redirect it through a url shortener, a custom one I use which would be something like this:
if you visited the actual url it would redirect you to a custom page where it displays the resulting short url, so that isn’t needed.
As you can tell, I’m not brilliant with PHP so any help would be great, also if you can see a better way of doing any of this please let me know, I’d like to improve my code.
Thanks!August 11, 2011 at 10:09 am #84829
Rather then using the rename() function you can set the new file name and path in a variable then when it comes time to using move_uploaded_file() all you need to do is reference the variable and you have the URL you need in the entire scope and the PHP core will do the rename for you. See the example below…
$random = substr(md5(rand(0, 9999)), 4, 10);
$file = '/home/my_site/public_html/images/' . $random . '.' . $_FILES;
As for the URL shortener i wouldn’t recommend adding the path to the file but a reference instead, so for instance if i wanted to see “image.dji32.jpg” i would use a URL such as..
The advantage to this is you can search for the specific file name in your folder or database and use a header return to hide the real URL.August 11, 2011 at 10:23 am #84832
so then if I used $file, would that give me the URL I need?
for example, using
would give me the full URL?August 12, 2011 at 12:04 am #84883
Outputting $file will give you an absolute path to the file on the server, if you want a full relative path that you can use the address bar you would need to remove “/home/my_site/public_html/” from $file. One thing that did make me twitch is that you do list the full path to the server which shouldn’t be needed as just
$file = 'images/' . $random . '.' . $_FILES;
Should allow the file to upload without any issues.August 12, 2011 at 4:33 am #84886
Thats quite strange, I tried using “/images” in my previous code and it wouldn’t work, after switching to yours, it now works fine, thanks for that!
Thanks for your help, do you by any chance know how to pass this into a URL shortener? More specifically Phurl? and then echo the resulting short URL?
Thanks!August 12, 2011 at 9:57 am #84912
Personally i would use bit.ly as it has a progressive API which is now in v3, you can find pre-built PHP API’s here but personally i prefer the API designed by Tijs Verkoyen which has some great documentation and is easy to implement.
Before you can use the API you need to get an account over at bit.ly which takes less then 1 minute if you use Facebook or Twitter, you can find information about the steps you need to take here.
- The forum ‘Back End’ is closed to new topics and replies.