Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Get the actual URL after a rename function

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #33877
    schotty
    Member

    Hi guys,

    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:


    print_r($_FILES);
    move_uploaded_file($_FILES["file"]["tmp_name"],"/home/my_site/public_html/images/".$_FILES["file"]["name"]);

    after that i added in:


    $random = substr(md5(rand(0,9999)), 4, 10);

    rename("/home/my_site/public_html/images/image.jpg", "/home/my_site/public_html/images/image.$random.jpg");

    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:

    http://example.com/index.php?alias=&url=example.com/images/image.dji32.jpg

    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!

    #84829
    SgtLegend
    Member

    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;

    print_r($_FILES);
    move_uploaded_file($_FILES, $file);

    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..

    http://www.example.com/index.php?alias=&url=image.dji32.jpg

    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.

    #84832
    schotty
    Member

    so then if I used $file, would that give me the URL I need?

    for example, using

    echo $file

    would give me the full URL?

    #84883
    SgtLegend
    Member

    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.

    #84886
    schotty
    Member

    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!

    #84912
    SgtLegend
    Member

    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.

    http://classes.verkoyen.eu/bitly/

    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.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.