Jump to content

Using Random Function, File Names


Lamez

Recommended Posts

How would I be able to use the random function to rename a file?

 

I have this

 

<?php
	$filename = $_FILES["file"]["name"];
	$newname = rand(100, 9000000);
                $new_filename =  rename($filename, $newname["name"]);
        $final = md5($new_filename);
?>

 

but I get this error:

 

Warning: rename(bb.png,) [function.rename]: No such file or directory in /mounted-storage/home48c/sub007/sc33591-LWQU/lamezz.info/user/usrava/new_upload.php on line 32

Uploaded

 

what am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/97335-using-random-function-file-names/
Share on other sites

what am I doing wrong?

Everything

 

1 ) First you need a physical file "bb.png" to rename. At the moment all you have is its name from the FILES array and a temprorary uploaded file. First move the uploaded temp file to $filename on your server

 

www.php.net/move_uploaded_file

 

2 ) What are you expecting in $newname["name"] ?

 

3 ) $new_filename will just contain either TRUE or FALSE 

 

www.php.net/rename

 

My script, I just wrote is suppose to check to see if the filename is a image, then it checks for spaces, if so replace them with _, then it is SUPPOSE to upload the file, then it generates a random number, renames that uploaded image with the new number, user, and old filename, then it takes that as a string and makes a hash encryption out of it, with the user, then it ends.

 

Well it does not upload the file! Why?

 

here is the code:

<?php 
include ("../../style/include/session.php");
$user = $session->username;
$uploaddir = '/';
?>

<html>
    <body>

    <form method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file" />
    <br/>
    <input type="submit" name="upload" value="Upload" />
    </form>

    </body>
    </html>

    <?php
if (isset($_POST['upload'])){

    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/bmp")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 104857600))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
	 $bfilename = $_FILES["file"]["name"];	
	 $filename = str_replace(' ', '_', $bfilename);
	 $uploaddir . basename($filename); 
         $newfile = $uploaddir . "/$filename";
	 $tmp = $_FILES["file"]["tmp_name"];

	 move_uploaded_file($uploaddir, $tmp);

	 $newname = rand(100, 9000000);
         $new_filename = rename($filename, $newname."-".$user."-".$filename);
     $final = md5($new_filename)."-".$user.".".getExt($filename);

	 echo "<br>";
	 echo "Before Name: ".$bfilename;
	 echo "<br>";
	 echo "Random Number: ".$newname;
	 echo "<br>";
	 echo "After Name: ".$new_filename;
	 echo "<br>";
	 echo "Hased Name: ".$final;
         echo "<br>Uploaded";
        }
      }
    else
      {
      echo "Invalid file";
      }
}

    function getExt($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; }
        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
        return $ext;
}
    ?>

Some errors here

<?php 
$bfilename = $_FILES["file"]["name"];	
$filename = str_replace(' ', '_', $bfilename);
$uploaddir . basename($filename);                // $_FILES[file][name] already contains basename only
$newfile = $uploaddir . "/$filename";            // newfile now contains "//filename"       !!!
$tmp = $_FILES["file"]["tmp_name"];

move_uploaded_file($uploaddir, $tmp);            // arguments are wrong way round  uploaddir should be newfile         !!!
?>

Also see my previous reply regarding rename()

alright, it is still not uploading, but would this work, if the file was uploaded?

<?php
        {
	 $bfilename = $_FILES["file"]["name"];	
	 $filename = str_replace(' ', '_', $bfilename);
	 $uploaddir . basename($filename); 
	 $tmp = $_FILES["file"]["tmp_name"];

	 move_uploaded_file($uploaddir, $filename);

	 $newname = rand(100, 9000000);
         $new_filename = rename($filename, $newname."-".$user."-".$filename);
     $final = md5($new_filename)."-".$user.".".getExt($filename);

	 echo "<br>";
	 echo "Before Name: ".$bfilename;
	 echo "<br>";
	 echo "Random Number: ".$newname;
	 echo "<br>";
	 echo "After Name: ".$new_filename;
	 echo "<br>";
	 echo "Hased Name: ".$final;
         echo "<br>Uploaded";
        }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.