I have a file with this name = "File in my computer.pdf";
When I try to scan the folder with the scandir function I get the error: The directory name is invalid.
I've tried to escape the filename with str_replace or with urlencode but I get that scandir can't find the filename.
This is the code:
$filename = "/folder/File in my computer.pdf";
scandir($filename);`
I've tried:
$filename = "/folder/File in my computer.pdf";
$filename = str_replace(" ", "\040", $filename);
scandir($filename);`
Related
I'm trying to copy an image from a URL to my server, but it doesn't save. Below is my code
$year ='2018';
$make = 'jeep';
$model = 'grand-cherokee';
$url = 'https://www.blabla.com/images/auctions/a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832';
$tmp_filename = explode('/images/auctions/', $url);
$filename = $tmp_filename[1].'.jpg';
//server path
$path = str_replace(' ', '-', strtolower(Yii::app()->basePath.'/www/storage/auction/us/'.$year.'/'.$make.'/'.$model.'/'.$filename));
$_path = pathinfo($path);
if (!file_exists($_path['dirname'])) {
mkdir($_path['dirname'], 0755, true);
}
//copy image to server
if (!is_file($path)) {
copy($url, $path);
//move_uploaded_file($url, $path);
}
The image URL in $url, doesn't have a file extension, but the image is a .jpeg
i tried both copy() and move_uploaded_file() but failed.
when i use copy() i get this error
copy(c:\xampp\htdocs\web\frontend/www/storage/auction/us/2018/jeep/grand-cherokee/a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832.jpg): failed to open stream: No such file or directory
I also tried with directory 2018/jeep/grand-cherokee exist and without too.
what am is wrong here? Thanks
found the problem, Guess the filename was too long? I got this error when i tried manually saving the image from browser to desktop
a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832
The File name. directory name or volume label syntax is incorrect
I renamed the file here
$url = 'https://www.blabla.com/images/auctions/a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832';
// $tmp_filename = explode('/images/auctions/', $url);
//$filename = $tmp_filename[1].'.jpg';
$filename = $year.'-'.$make.'-'.$model.'.jpg';
//server path
$path = str_replace(' ', '-', strtolower(Yii::app()->basePath.'/www/storage/auction/us/'.$year.'/'.$make.'/'.$model.'/'.$filename));
and it works now.
I am trying to upload a file and rename that file to include the folder name, and the folder name contains white space.
After I added the folder name to filename the output filename now includes '_' characters.
How can I remove those _?
I am doing it like this
$new_name = $album.' PF';
$config['file_name'] = $new_name;
I am getting the filename back like this
GAYATRI_EDUCATIONAL_SOCIETY_PF.xls
$name="the_first_image.jpeg";
str_replace("_", "", $name);
will work for you
Use str_replace to remove _ form file name as
$str="GAYATRI_EDUCATIONAL_SOCIETY_PF.xls";
echo str_replace("_", "", $str);
You can use str_replace to find and replace from string.
$new_name = $album.' PF';
$new_name = str_replace('_', '', $new_name);
$config['file_name'] = $new_name;
Try String Replace Function Of Php
$config['file_name'] = str_replace("_", "",$new_name);
Very simple,
Use str_replace PHP function
str_replace(find,replace,string,count)
example - $new_name = str_replace('_', '', $new_name);
This will definitely do the magic
execute this code in the terminal
find . -type f -name '._*' -delete
I've set of URLs each one ended with file name that has extension to its type
Example
$link = "http://www.some_site.com/test.vob";
Using PHP i wonder how can i get the extension of the last file name on the link so i wanna get vob.
I've tried this one but it gives the file name itself test but i want the extension vob
$filename = basename($link);
$filename = substr($filename, 0, strpos($filename, '.'));
echo $filename; // test
~ Thanks
Take a look at pathinfo():
$link = "http://www.some_site.com/test.vob";
echo pathinfo($link, PATHINFO_EXTENSION); // vob
Example: http://3v4l.org/oQn3c
Hello I am using the following code to upload files on my server and to write the filename inside the database. My question is how I can achive when the file is uploaded the name of the file to be changed ? Right now I am facing a problem if the filename have space sbetween the words if it is not a whole word the file is not uploading correctly.
here is the code:
$target = "../images/";
$target = $target . basename( $_FILES['photo']['name']);
$filename = $_FILES['photo']['name'];
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
//resize function
createThumbnail($filename);
You need to parse the filename removing all unwanted characters. The easiast way is to use preg_replace forbidden characters. Here's an example:
// List of forbidden chars (\ and / need to be escaped with \)
$forbiddenChars = "\/\\?%*:|\"<>. ";
//Characters not allowed are replaced by this var
$replaceStr = "_";
$filename = preg_replace("/[$forbiddenChars]/", $replaceStr, $_FILES['photo']['name']);
//DEBUG: Test if everything is ok (should be deleted in production)
var_dump($filename);
SORRY CAUGHT MY OWN ERROR AFTER POST
I have this code that i am running after a HTML from uploads a file to my .uploadAdmin.php section. Im having a lot of trouble with this.
$clientname = $_SESSION['MM_USERNAME'];
$extension = end(explode(".", $_FILES["file"]["name"]));
$myText = (string)$_FILES["file"]["name"];
$myText = str_replace("'", "", $myText);
print $myText; // This does come out with no '
My question is how do i change to file name after i have a new file name with no ' I have tried a few things including
$_FILES["file"]["name"] = $myText;
I had a comparison operator == instead of =,
Thanks Amy :)
Once you've uploaded your file you need to move it from the temporary directory to the place you want to store it. This is a security feature. The uploaded file will have a temporary file name. You can change the real filename as part of the move_uploaded_file() operation.
// where to store file
$uploads_dir = '/uploads';
// name of temporary file after upload.
$tmp_name = $_FILES["file"]["tmp_name"];
// original name of file, with apostrophes replaced
$name = str_replace("'", "", $_FILES["file"]["name"]);
// move the file
if (move_uploaded_file($tmp_name, "$uploads_dir/$name") === false) {
// do something if the file isn't an uploaded file.
}
Read the PHP reference Handling File Uploads for the complete reference.