First letter from file name not good - php

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

Related

Integer in while loop wont increase value

I'm having a problem in my code, I am trying to append a number to a filename if filename already exists. It goes something like this
$explode = explode(".", $fileName);
$extension = end($explode);
$fileactualname = reset($explode);
$i = 0;
while (file_exists($location.$fileName)) {
$i++;
}
$fileName= $i.$fileName;
$name = $fileName;
$moveResult = move_uploaded_file($fileTmpLoc, $location . "/". $name);
if ($moveResult != true) {
#unlink($fileTmpLoc);
header('location: ' . URL . '?page=0&sort=name&type=desc&folder=uploads/&message=uploaderror');
}
Unfortunately for some reason $i wont increase its value by 1 every time it loops, instead it adds to the filename this way 1234filename.jpg my file name variable is after the loop and i cant understand why this is accruing. I am expecting to get ($i)filename.jpg a single number
AFTER RESTARTING MY LOCALSERVER IT STARTED WORKING WITH THE CODE PROVIDED BELOW DUUUH
You need to use the actual filename when you concat the number to it and not the one you already added a number to.
// not sure why you are splitting the filname up here
$explode = explode(".", $fileName);
$extension = end($explode);
$fileactualname = reset($explode);
$i = 0;
$fn = $fileName;
while (file_exists($location.$fn)) {
$i++;
// add number to actual filename
$fn = $i.$fileName;
}
$name = $fn;
$moveResult = move_uploaded_file($fileTmpLoc, $location . "/". $name);

How to remove space?

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;

PHP base64_docode saves corrupt image

I am sending base64 encoded image to PHP from my android app. Sometime it stores full image (4KB) and sometime (3KB) (Same Image). when I use URL in picasso, image with 4KB size works fine but image with 3KB size does not load it shows decode error.
This is my PHP code (which sometime works)
$encodedImage = str_replace(' ','+',$_POST['encodedProfileImage']);
$data = base64_decode($encodedImage);
$file = 'Pics/'. uniqid() . '.png';
$success = file_put_contents($file, $data);
$BASE_URL = 'http://domain.com/TestApp/';
I then do SQL operation in PHP to store image path. Is there any chance that next code operation is done on half decoded image(which is corrupt).
You need to remove the part that says data:image/png;base64, at the beginning of the image data. The actual base64 data comes after that.
Use below function:-
function base64_to_png($base64_string, $output_file) {
$ifp = fopen($output_file, "wb");
$data = explode(',', $base64_string);
fwrite($ifp, base64_decode($data[1]));
fclose($ifp);
return $output_file;
}
If you want to use str_replace function then may be below way work. I am not sure :)
$fname = filter_input(INPUT_POST, "name");
$encodedImage = filter_input(INPUT_POST, "image");
$encodedImage = str_replace('data:image/png;base64,', '', $encodedImage);
$encodedImage = str_replace(' ', '+', $encodedImage);
$encodedImage = base64_decode($encodedImage);
file_put_contents($fname, $encodedImage);
print "Image has been saved!";
Hope it will help you :)

php move_uploaded_file not working when filename has spaces

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);

Uploading a csv file in PHP. Where is the error in the code

<?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;

Categories