Jump to content

Recommended Posts

Well i have this upload bar code that i was just wondering how i would be able to manipulate this code i have here to upload the file, then give it a different name to it but still keep the extension on the end like if i uploaded a file called 103_2893.jpg... it would rename it to example.jpg

 

then i would need to be able to overwrite it if there is a file in there already with that name

 

<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "upload/".$HTTP_POST_FILES['ufile']['name'];
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";

//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
echo "<img src=\"$path\" width=\"150\" height=\"150\">";
}
else
{
echo "Error";
}
}
?>

 

help is greatly appreciated thanks

Link to comment
https://forums.phpfreaks.com/topic/78126-solved-upload-bar-enhanced/
Share on other sites

This should work

 

<?php
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

//This applies the function to your file
$ext = findexts ($_FILES['uploaded']['name']) ; 
//This is your new file name
    $new = "example";

//This adds a . on the end, so it is ready of the file extension to be appended.
    $new2 = $new.".";

//This assigns the subdirectory you want to save into
    $target = "images/";

//This combines the directory, the file name, and the extension
    $target = $target . $new2.$ext; 

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file has been uploaded as ".$new2.$ext."<br />";
//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "Original File Name :".$_FILES['uploaded']['name']."<BR/>";
echo "File Size :".$_FILES['uploaded']['size']."<BR/>";
echo "File Type :".$_FILES['uploaded']['type']."<BR/>";
echo "<img src=\"$target\" width=\"150\" height=\"150\">";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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