code:
$filename = 'college_logo/'.$college_name22[0].'.jpg';
echo $filename;
In this code I want to remove space between two words. here when I echo $filename it print college_logo/Christian Medical College, Vellore .jpg
In this name having space between (vellore .jpg) but I want (vellore.jpg)
how can I fix this problem ?
Thank You
Change from
$filename = 'college_logo/'.$college_name22[0].'.jpg';
to
$filename = 'college_logo/'.trim($college_name22[0]).'.jpg';
You can replace spaces width underscore "_" using str_replace(" ", "_",$filename);
$filename = 'college_logo/'.$college_name22[0].'.jpg';
$filename = str_replace(" ", "_",$filename);
echo $filename;
Related
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 am uploading a file to the server. everything works fine unless the uploaded file has a space in it.
I tried to use:
str_replace(" ", "_", $_FILES['image']['name']);
My code is
$image_name= str_replace(" ", "_", $_FILES['image']['name']);
$image_tmp_name = $_FILES['image']['tmp_name'];
$image=$_FILES['image'];
$url = "http://jkshahclasses.com/push_images/$image_name";
if(move_uploaded_file($image_tmp_name,"../../push_images/$image_name"))
{
echo "file uploaded";
}
else
{
echo "error: file not uploaded";
}
Thanks in advance
You can use this function to slugify your file name before uploded :
public function slugify($text)
{
// replace all non letters or digits by _
$text = preg_replace('/\W+/', '_', $text);
// trim and lowercase
$text = strtolower(trim($text, '_'));
return $text;
}
But first you have to get only file name you can do it with this function :
$file_name = pathinfo($path, PATHINFO_FILENAME);
If you want to get file extension you have to use :
$file_ext = pathinfo($path, PATHINFO_EXTENSION);
I use UTF-8 letters. It's works fine, but if first letter from file is "č", "ž", "š"... not work good.
File name is: ča ča ča.doc
I have this code:
$name1 = $_FILES["file"]["name"]; //here is ča ča ča.doc
$ext = pathinfo($name1, PATHINFO_EXTENSION); //here is doc
$name = basename($name1, $ext); //here is "a ča ča." missing first letter
This not work only if here is first leter "č,š,ž,đ..."
PHP iconv()
Try to encode the $name1 yet again to a different encoding, Windows-1252.
//encode to windows-1252 to save to the filesystem
$name1 = $_FILES["file"]["name"]; //here is ča ča ča.doc
$encoded_filename = iconv("UTF-8","Windows-1252//IGNORE",$name1);
or CP858
$name1 = $_FILES["file"]["name"]; //here is ča ča ča.doc
$encoded_filename = iconv("UTF-8", "CP858//IGNORE", $name1)
After 30 min r&d on this, i found, you could try this:
$search = array('š','á','ž','í','ě','é','ř','ň','ý','č',' ');
$replace = array('s','a','z','i','e','e','r','n','y','c','-');
$code_encoding = "UTF-8"; // this is my guess, but put whatever is yours
$os_encoding = "CP-1250"; // this is my guess, but put whatever is yours
$name1 = $_FILES["file"]["name"];
$name1 = iconv($os_encoding , $code_encoding, $name1); // convert before replace
$name1 = str_replace($search, $replace, $name1);
Reference: https://stackoverflow.com/a/1767011/2236219
<?php
require '../config.php';
// Edit upload location here
$result = 0;
$name = mysql_real_escape_string($_FILES['myfile']['name']);
$path = csv;
$ext = 'csv';
$md5 = md5($name);
$target_path = $path . '\\' . $md5 . '.' . $ext;
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
$result = 1;
}
sleep(1);
?>
It won't upload any files such as with the file names that contain brackets, etc.
Don't do:
$name = mysql_real_escape_string($_FILES['myfile']['name']);
Do:
$name = $_FILES['myfile']['name'];
you are getting the MD5 of $name so no reason to clean it as it will produce a 32-char hex string which will not contain any special characters regardless. If a filename contains special chars and you escape using the above the MD5 will completely change.
The error is likely here:
$path = csv;
Here PHP is looking for a constant with name csv unless you have defined that, it is going to return null, so your $target_path is not being built correctly.
Just one more thing to try is use the pre defined DIRECTORY_SEPERATOR constant while building your $target path so...
$target_path = $path . DIRECTORY_SEPERATOR . $md5 . '.' . $ext;
I need your help with a RegEx in PHP
I have something like:
vacation.jpg and I am looking for a RegEx which extracts me only the 'vacation' of the filename.
Can someone help me?
Don't use a regex for this - use basename:
$fileName = basename($fullname, ".jpg");
You can use pathinfo instead of Regex.
$file = 'vacation.jpg';
$path_parts = pathinfo($file);
$filename = $path_parts['filename'];
echo $filename;
And if you really need regex, this one will do it:
$success = preg_match('~([\w\d-_]+)\.[\w\d]{1,4}~i', $original_string, $matches);
Inside matches you will have first part of file name.
Better answers have already been provided, but here's another alternative!
$fileName = "myfile.jpg";
$name = str_replace(substr($fileName, strpos($fileName,".")), "", $fileName);
You don't need regex for this.
Approach 1:
$str = 'vacation.jpg';
$parts = explode('.', basename($str));
if (count($parts) > 1) array_pop($parts);
$filename = implode('.', $parts);
Approach 2 (better, use pathinfo()):
$str = 'vacation.jpg';
$filename = pathinfo($str, PATHINFO_FILENAME);