How to rename file for each download request

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fogsys
    New Member
    • Apr 2010
    • 13

    How to rename file for each download request

    ok here is what Im trying to create:
    simple code that renames the file to be downloaded each time someone downloads it.
    example:
    you download my file, it will be rename to smth different that way you cant access the file using that URL anymore

    here is the code:
    for some reasons the renaming part doesnt work.
    any input would be greatly appreciated
    as122223333443. txt = the new generated name is updated to this file that way the next person to download gets the right link.

    Code:
    <?php
    function renom($file1, $file2)
    {
    	rename($file1, $file2);
    	}
    function updateit()
    {
    	$random_digit=rand(0000,9999);
    	$random_link = $random_digit.".zip";
    	$myFile = "as122223333443.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $random_link;
    fwrite($fh, $stringData);
    fclose($fh);
    $file1 = $thelink;
    $file2 = $random_link;
    renom($file1,$file2);
    	}
    function downloada()
    {
    	$dlink = "as122223333443.txt";
    $fh = fopen($dlink, 'r');
    $thelink= fread($fh, 25);
    fclose($fh);
    
    echo "http://www.site.com/".$thelink;
    	updateit();
    	}
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Originally posted by fogsys
    ok here is what Im trying to create:
    simple code that renames the file to be downloaded each time someone downloads it.
    example:
    you download my file, it will be rename to smth different that way you cant access the file using that URL anymore

    here is the code:
    for some reasons the renaming part doesnt work.
    any input would be greatly appreciated
    as122223333443. txt = the new generated name is updated to this file that way the next person to download gets the right link.

    Code:
    <?php
    function renom($file1, $file2)
    {
    	rename($file1, $file2);
    	}
    function updateit()
    {
    	$random_digit=rand(0000,9999);
    	$random_link = $random_digit.".zip";
    	$myFile = "as122223333443.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $random_link;
    fwrite($fh, $stringData);
    fclose($fh);
    $file1 = $thelink;
    $file2 = $random_link;
    renom($file1,$file2);
    	}
    function downloada()
    {
    	$dlink = "as122223333443.txt";
    $fh = fopen($dlink, 'r');
    $thelink= fread($fh, 25);
    fclose($fh);
    
    echo "http://www.site.com/".$thelink;
    	updateit();
    	}
    Your variable $thelink is invisible to your function updateit. I suggest passing your $thelink variable to your updateit function.

    Comment

    Working...