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);
Related
I created a page that can upload file to my database, but when a filename has (.), it doesnt save properly. For example I upload a file named imagefile.50.jpg, it just saves as image20.50
<?php
function upload_image()
{
if(isset($_FILES["user_image"]))
{
$extension = explode('.', $_FILES['user_image']['name']);
$new_name = $extension[0] . '.' . $extension[1];
$destination = './upload/' . $new_name;
move_uploaded_file($_FILES['user_image']['tmp_name'], $destination);
return $new_name;
}
}
To get the filename and extension of a file, you can use pathinfo, i.e.:
$file = "some_dir/somefile.test.php"; # $_FILES['user_image']['name']
$path_parts = pathinfo($file);
$fn = $path_parts['filename'];
$ext = $path_parts['extension'];
print $fn."\n";
print $ext;
Output:
somefile.test
php
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;
i am using this code to save the uploaded file but the problem is it is allowing the file with name with special character and space.
for example it is allowing
hi how are you
but i dont want to allow any space ,special charater etc. here is my code .i tried with preg_replace in uri but after that i tried to upload file but nothing got uploaded.
function save_file($file) {
$allowed_ext = array('jpg','png','gif','jpeg');
$ext = $file['name'];
$ext = strtolower($ext);
if (in_array($ext, $allowed_ext)) {
die('Sorry, the file type is incorrect:'.$file['name']);
}
$fname = date("H_i",time()).'_'.get_rand(5);
$dir = date("Ym",time());
$folder = 'uploads/userfiles/'.$dir;
$uri = $folder.'/'.$fname.'.'.$ext;
if (!is_dir($folder))
mkdir($folder, 0777);
if (copy($file['tmp_name'],$uri))
return $uri;
else {
return false;
}
}
To strip non letters out of a string you can use the following regular expression
$input = preg_replace("/[^a-zA-Z]+/", "", $input);
I have a basic script that allows my site members to upload short samples of their compositions for customers to listen to.
Some file names have spaces which obviously causes problems when trying to play the samples. Is there a way to replace these spaces with underscores between uploading to the temp directory and moving the file to the 'samples' directory?
<?
$url = "http://domain.com/uploads/samples/";
//define the upload path
$uploadpath = "/home/public_html/uploads/samples";
//check if a file is uploaded
if($_FILES['userfile']['name']) {
$filename = trim(addslashes($_FILES['userfile']['name']));
//move the file to final destination
if(move_uploaded_file($_FILES['userfile']['tmp_name'],
$uploadpath."/". $filename)) {
echo "
bla bla bla, the final html output
";
} else{
if ($_FILES['userfile']['error'] > 0)
{
switch ($_FILES['userfile']['error'])
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}
}
}
?>
After this line:
$filename = trim(addslashes($_FILES['userfile']['name']));
Write:
$filename = str_replace(' ', '_', $filename);
A filename like hello world.mp3 (two spaces) would come out as hello__world.mp3 (two underscores), to avoid this you could do this instead:
$filename = preg_replace('/\s+/', '_', $filename);
$filename = str_replace(' ', '-', trim(addslashes($_FILES['userfile']['name'])));
Why addslashes though? This also seems a little too simple -- am I missing something?
try;
$filename = trim(str_replace(" ","_", $_FILES['userfile']['name']));
i'd like to change the name of uploaded file to md5(file_name).ext, where ext is extension of uploaded file. Is there any function which can help me to do it?
$filename = basename($_FILES['file']['name']);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new = md5($filename).'.'.$extension;
if (move_uploaded_file($_FILES['file']['tmp_name'], "/path/{$new}"))
{
// other code
}
Use this function to change the file name to md5 with the same extension
function convert_filename_to_md5($filename) {
$filename_parts = explode('.',$filename);
$count = count($filename_parts);
if($count> 1) {
$ext = $filename_parts[$count-1];
unset($filename_parts[$count-1]);
$filename_to_md5 = implode('.',$filename_parts);
$newName = md5($filename_to_md5). '.' . $ext ;
} else {
$newName = md5($filename);
}
return $newName;
}
<?php
$filename = $_FILES['file']['name'];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new = rand(0000,9999);
$newfilename=$new.$filename.$extension;
if (move_uploaded_file($_FILES['file']['tmp_name'],$newfilename))
{
//advanced code
}
?>
Find below php code to get file extension and change file name
<?php
if(isset($_FILES['upload_Image']['name']) && $_FILES['upload_Image']['name']!=='') {
$ext = substr($_FILES['upload_Image']['name'], strpos($_FILES['upload_Image']['name'],'.'), strlen($_FILES['upload_Image']['name'])-1);
$imageName = time().$ext;
$normalDestination = "Photos/Orignal/" . $imageName;
move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination);
}
?>
This one work
<?php
// Your file name you are uploading
$file_name = $HTTP_POST_FILES['ufile']['name'];
// random 4 digit to add to our file name
// some people use date and time in stead of random digit
$random_digit=rand(0000,9999);
//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables
$new_file_name=$random_digit.$file_name;
//set where you want to store files
//in this example we keep file in folder upload
//$new_file_name = new upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "upload/".$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";
//$new_file_name = new file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$new_file_name."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
}
else
{
echo "Error";
}
}
?>