i'm in trouble this is an example of $_FILES['file']['tmp_name']
/Applications/XAMPP/xamppfiles/temp/phpjCag18
i would like to get the temp binary filename so in this case phpjCag18
Is there anyway to get it from the $_FILES[]?
Ho to get it clean from path ?
You can use basename() for this purpose.
$tmp = $_FILES['file']['tmp_name'];
echo basename( $tmp );
Or, if you're trying to get the extension, use pathinfo():
$extension = strtolower( pathinfo($_FILES['file']['tmp_name'], PATHINFO_EXTENSION) );
If I understand you correctly:
basename($_FILES['file']['tmp_name'])
Related
I need to upload two files with different ext example :
file_1.txt and file_image.png
when the user sent the two files using upload an form :
file_1.txt and file_image.png
before move_uploaded_file I need to rename the image name to be like the txt. name file :
file_1.txt
file_1.jpeg
I just want to rename the image filename, not the extension
Code is something like this :
switch($_REQUEST['action']) {
case "upload":
$Imagefile_temp = $_FILES['file']['tmp_name'];
$Textfile_temp = $_FILES['file']['tmp_name'];
$Imagefile_name = $_FILES['file']['name'];
$Textfile_name = $_FILES['file']['name'];
$ImageFileType = array('png' ,'jpg');
$TextFileType = array('txt');
$Image_extension = pathinfo($Imagefile_name, PATHINFO_EXTENSION); // holds the file extension of the file
$Text_extension = pathinfo($Textfile_name, PATHINFO_EXTENSION); //holds the file extension of the file
// //Declaring Path for uploaded images
$file_path = "uploads";
//checks for duplicate files
if((!file_exists($file_path."/".$Imagefile_name)) || (!file_exists($file_path."/".$Textfile_name))) {
$j = 0; //Variable for indexing uploaded image
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
if(in_array($Image_extension,$ImageFileType) ) {
$Image_extension = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.)
$file_extension = end($Image_extension); //store extensions in the variable
$target_path = $target_path . md5(uniqid()) . "." . $Image_extension[count($Image_extension) - 1];//set the target path with a new name of image
$j = $j + 1;//increment the number of uploaded images according to the files in array
$Imagefilestatus = move_uploaded_file($Imagefile_temp,$file_path."/"."image_".$Imagefile_name) ;//if file moved to uploads folder
}
if(in_array($Text_extension,$TextFileType) ) {
$Text_extension = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.)
$file_extension = end($Text_extension); //store extensions in the variable
$target_path = $target_path . md5(uniqid()) . "." . $Text_extension[count($Text_extension) - 1];//set the target path with a new name of image
$j = $j + 1;//increment the number of uploaded images according to the files in array
$Textfilestatus = move_uploaded_file($Textfile_temp,$file_path."/".$Textfile_name) ;//if file moved to uploads folder
}
}
}
}
$name = 'Whatevernameyouwant.'.pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
pathinfo with parameter PATHINFO_EXTENSION returns the file extension.
You can just explode the text file from the extension and use it as the image file name when moving it :
$text_file_name = $_FILES["text"]["name"]; // give you the text file name
$image_file_name = $_FILES["image"]["name"]; // give you the image file name
$file_expensions = strtolower(end(explode(".", $image_file_name))); // give you the image extension and so you can use it later for saving the file
$n = explode(".", $text_file_name, 1);
$name = $n[0]; // get text file name
$name = $name.".".$file_expensions; // this is new name of the image
move_uploaded_file($file_tmp, "path/$name")
I assume you are aware that the code you've presented here allows anyone with upload privileges to upload and execute PHP code.
(the reason I mention this is that there is scope for a lot of improvement elsewhere - and if the security vulnerability is exploitable then you should really think about start again from scratch with this).
Will your extension always be .jpg or .png ? (what about .jpeg ?)
I need to rename the image name to be like the txt. name file
I presume you mean that you want the filename on the retained image file to match that of the txt file - you can specify the destination name in move_uploaded_file(). Breaking down a process into steps is a good idea for managing complexity but file operations are expensive and here redundant.
So really you want to know how to get the a filename without an extension then add the file extension from a different file.
You already have the extension you want to apply in $Image_extension
While you could just do....
$destination_name=$destination_path
. str_replace('.txt', '.' . $image_extension, $Textfile_name);
However this will not work as expected if $imageFile_name contains more than one instance of '.txt'.
You could use PCREs to specify that you should only apply the change to the end of the string:
$destination_name=preg_replace("/txt$/i", $image_extension, $Textfile_name);
Or take the last 4 characters off $Textfile_name then append the image extension.
$destination_name=substr($Textfile_name, 0, -3) . $image_extension;
Or strrev the text file name, replace 'txt' with strrev($image_extension) with a limit of 1, then strrev the result to ensure only the end of the string is replaced.
If I spent more time thinking about this, there are probably other solutions too - but they will become increasingly esoteric.
In general, allowing users to pick their own names (or even part thereof) for content uploaded inside the document root is a bad idea.
I want to rename all files in a folder with random numbers or characters.
This my code:
$dir = opendir('2009111');
$i = 1;
// loop through all the files in the directory
while ( false !== ( $file = readdir($dir) ) ) {
// do the rename based on the current iteration
$newName = rand() . (pathinfo($file, PATHINFO_EXTENSION));
rename($file, $newName);
// increase for the next loop
$i++;
}
// close the directory handle
closedir($dir);
but I get this error:
Warning: rename(4 (2).jpg,8243.jpg): The system cannot find the file specified
You're looping through files in the directory 2009111/, but then you refer to them without the directory prefix in rename().
Something like this should work better (though see the warning about data loss below):
$oldName = '2009111/' . $file;
$newName = '2009111/' . rand() . (pathinfo($file, PATHINFO_EXTENSION));
rename($oldName, $newName);
Of course, you may want to put the directory name in a variable or make other similar tweaks. I'm still not clear on why you're trying to do this, and depending on your goals there may be better ways of reaching them.
Warning! The approach you are using could cause data loss! A $newName could be generated that is the same name as an existing file, and rename() overwrites target files.
You should probably make sure $newName doesn't exist before you rename().
I have a folder where Im uploading images of different format.The file extension can be jpg/png/gif.
Eg : id_proof.png
id_proof.jpg
id_proof.gif
but the file name remains the same.Now how can I show up this file by just the name of the file.Right now what I am doing is
$path1 = "/var/www/html/merchant/images/".$merchant_user_id."/id_proof.png";
but now that the file extension can be either of the 3 , how will I be able to read it without a file extension ?Please help out.
User path inside a loop:
$extensions = array( 'jpg', 'png', 'gif' );
foreach( $extensions as $ext ){
$path = "/var/www/html/merchant/images/" . $merchant_user_id . "/id_proof." . $ext;
if( is_file( $path ) ){
// do something with path
}
}
Have you Tried basename() in php.
basename ( $path , $suffix );
I suppose there's a couple ways you could handle this:
Include the path to the id_proof image file in your data for each merchant.
If the UI has a preview associated with the image upload, you could have that preview be a canvas element and then force it to send a png to the server every time.
After upload, you could use something like the gd library to convert the image into a standard file type (all jpg, or all png).
I have some images on my system that were saved without a file extension, they appear in the format of "FILENAME." (Note the period)
I am trying to use getimagesize(); but it is erroring out telling me that "Filename cannot be empty"
Here is a sample of the code I am using
$contents = file_get_contents($localFile);
$imageData = getimagesize($contents);
// $imageData[2] will contain the value of one of the constants
$mimeType = image_type_to_mime_type($imageData[2]);
$extension = image_type_to_extension($imageData[2]);
Look at the documentation: http://php.net/getimagesize
getimagesize() is expecting a filename as first parameter, not the contents of the image file.
Try:
$imageData = getimagesize($localFile);
// $imageData[2] will contain the value of one of the constants
$mimeType = image_type_to_mime_type($imageData[2]);
$extension = image_type_to_extension($imageData[2]);
getimagesize expects a file name as the first argument
I'm writing the code for a website right now that's uploads images and then displays them gallery style. What I would like to happen is for the file name of the image to be entered into the site's database as the name of the image. However, just using $_FILES['images']['name']; gives me the file name but with the file extension attached at the end. How would I remove the file extension so I can use the file name by itself?
You can use the pathinfo() function (docs).
$example = "my_file.jpeg";
$filename = pathinfo($example, PATHINFO_FILENAME);
echo $filename; // my_file
Just use preg_replace:
$name = preg_replace('/(.+)\.\w+$/U', $_FILES['images']['name'], '$1');
You can use rename to remove the extension:
-> http://www.php.net/manual/en/function.rename.php
As my comment above implies, it depends on what you consider in the filename to be the name and what is the extension.
everything up to the last dot:
$filename = 'some.file.name.zip';
$name = substr($filename, 0, strrpos($filename, '.'));
everything up to the first dot:
$filename = 'some.file.name.zip';
$name = substr($filename, 0, strpos($filename, '.'));
they look the same, but the first one looks for the first dot from the end of the string and the second one from the start of the string.