Page 1 of 1

Upload script + renaming

Posted: Sun Jun 18, 2006 12:35 pm
by Qaid
Hi,
I found this script:

Code: Select all

<?PHP

//If the submit button is pressed:

if($_POST['submit'])

{

//Change this to the directory you would like to upload files to.

$directory = "upload/";



//Change this to the max file size you want users to be able to upload - File size is in bytes (1,024 Bytes = 1 Kilobyte (KB))

$max_file_size = "204800";



//Change these to allowed files types users can upload

$allowedfile[] = "image/gif"; //.gif

$allowedfile[] = "image/jpeg"; //.jpeg

$allowedfile[] = "image/jpg";  //.jpg

$allowedfile[] = "image/png";  //.png





//Check max file size

if (is_uploaded_file($_FILES["file"]["tmp_name"])) {

if($_FILES["file"]["size"]>$max_file_size) {

$is_uploaded = "failed";

echo 'Sorry, this file is too large. The maximum filesize is '.$max_file_size.' bytes, although your file is '.$_FILES["file"]["size"].'. ';

exit(); //If $is_upload = failed, then we stop the uploading process

}



//Check file type

if(!in_array($_FILES["file"]["type"],$allowedfile)) {

$is_uploaded = "failed";

echo 'Sorry, wrong file type, "'.$_FILES["file"]["type"].'" is not allowed. ';

exit(); //If $is_upload = failed, then we stop the uploading process

}



//Check to see if file exists | If it does, then we stop the process, although if it doesnt then we continue

if(file_exists($directory.$_FILES["file"]["name"])) {

$is_uploaded = "failed";

echo 'Sorry, this file already exists. ';

exit(); //If $is_upload = failed, then we stop the uploading process

}

//Now, if $is_uploaded does NOT = failed, we remove invalid characters in the filename, and replace all spaces with underscores

    if($is_uploaded!="failed") {

        $replace = array("$","%","#","@","!","&","^","*","(",")","-");

        $new = str_replace($replace,"",$_FILES["file"]["name"]);

        $fileName = str_replace(" " , "_" , $new);



            //If the directory defined in the beggining does not exist, we create it and CHMOD it to 777

            if(! is_dir($directory)){

                mkdir($directory,0777);

            }

             //Now we upload the file and check for errors

            if (move_uploaded_file($_FILES["file"]["tmp_name"], $directory.$fileName)) {

echo "Your file, $fileName has successfully been uploaded!  Click <a href=\"{$directory}{$fileName}\">Here</a> to view your file.";                     }

                         else {

        echo 'Sorry, your file has not uploaded.';

        exit();

        }

    }

} else {

    echo 'There has been an unknown error while uploading';

    exit();

}

}

?>

<!---Submit File Form--->



      <center>

        <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">

          <table>

            <tr>

              <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Choose File:</font></td>

              <td><input type="file" name="file">

                <input name="submit2" type="submit" value="Upload File">

</td>

              <input type="hidden" name="submit" value="true">

            </tr>

            </table>

        </form>

      </center>

<!---End Submit File Form--->
But how can i configure it so the uploaded file will automatically be renamed?
Please note that i'm not a shark :)

Thank for your help.
Best Regards,
Qiad

Posted: Mon Jun 19, 2006 3:16 am
by twigletmac
This is the section of the code you are interested in:

Code: Select all

//Now we upload the file and check for errors 
if (move_uploaded_file($_FILES["file"]["tmp_name"], $directory.$fileName)) {

echo "Your file, $fileName has successfully been uploaded!  Click <a href=\"{$directory}{$fileName}\">Here</a> to view your file.";
}
Basically - to rename the file you would need to change the destination in the call to move_uploaded_file(). How you do it is of course going to depend on how exactly you intend to rename the file.

Mac

Posted: Tue Jun 20, 2006 6:39 am
by Qaid
Hi again,
i looked at move_uploaded_file() but couldnt find what i was looking for.

I want this to happen:
- when i upload the file with the name: test.jpg it should rename it to: prefix_w35sw4.jpg or something random after the prefix_"xxx".jpg

But i dont know how :(

Thanks again.

Posted: Tue Jun 20, 2006 7:39 am
by Grim...
This will check that you're not overwriting something that already exists...

Code: Select all

$fileName = "prefix_".rand(0, 9).".jpg";
while (file_exists($fileName)) {
    $fileName = substr($fileName, 0, -4).rand(0, 9).".jpg";
}

if (move_uploaded_file($_FILES["file"]["tmp_name"], $directory.$fileName)) {

echo "Your file, $fileName has successfully been uploaded!  Click <a href=\"{$directory}{$fileName}\">Here</a> to view your file.";
}