php weird file_exists bug - php

Has anyone got any idea to why doesn't the following work ?
$file = 'images/thumbs/1%20-%20Copy.jpg';
if(!file_exists($file)){
die('NOT THERE');
}
echo 'Yes its there.';
The problem is with the spaces. I have checked the file exists,dbl checked n triple checked im going nuts. :(
Help

file_exists works on the file system and not via HTTP. So %20 will not be recognized as space but literally as %20; use spaces instead:
$file = 'images/thumbs/1 - Copy.jpg';

$file = rawurldecode('images/thumbs/1%20-%20Copy.jpg');

try these two
$file = 'images/thumbs/1\ -\ Copy.jpg';
$file = 'images/thumbs/1 - Copy.jpg';

Related

Renaming files from blanc space between name to underscore with php

i am trying to find a solution for the following problem:
I have two kinds of files in a folder of my webserver:
locked files__date_time.txt
locked files_date_time.txt
I would like to rename the files of the second type with the single underscore between "filename" and "date" like "locked files_date_time.txt" to "locked_files_date_time.txt" with a simple php-script.
This for i found a sample script, but its not working.
<?php
$directory = '/daten/www/htdocs/files/locked/';
foreach (glob($directory."*locked files*") as $filename) {
$file = realpath($filename);
rename($file, str_replace("*locked files*","locked_files",$file));
}
?>
Script is geeting executed, without error, but nothing happens with the files.
What is wrong there?
Would be pleased about some support. Thanks very much in advance.
Greetz
To update the file names this line needs to be updated:
Old:
str_replace("*locked files*","locked_files",$file));
New:
str_replace("locked__files","locked_files",$file));
To find the files that you are looking for this line needs to be updated:
Old:
foreach (glob($directory."*locked files*") as $filename) {
New:
foreach (glob($directory."*locked__files*") as $filename) {
That is looking for files that match what is between the double quotes.
<?php
$directory = '/daten/www/htdocs/files/locked/';
foreach (glob($directory."*locked files*") as $filename) {
$file = realpath($filename);
rename($file, str_replace("locked__files","locked_files",$file));
}
?>
It will solve your problem
rename($file, preg_replace("/locked files/","locked_files",$file));

PHP unlink(); No such file or directory

I searched everywhere for this problem and can't find the solution. I have this:
<?php
$file_name = $_GET['name'];
$file_delete = '../u/' . $file_name;
unlink($file_delete);
//header("location: $file_delete");
?>
unlink returns the error: No such file or directory, but if I try header("location: $file_delete"); it opens the file (picture in this case).
Where may I be wrong?
Get Absolute path first for the file to be deleted and check file exist before delete:
$file_name = $_GET['name'];
$base_dir = realpath($_SERVER["DOCUMENT_ROOT"]);
$file_delete = "$base_dir/your_inner_directories_path/$file_name";
if (file_exists($file_delete)) {unlink($file_delete);}
After some research, unlink() doesn't seem to allow you to use relative paths (with "../").
Here's an alternative:
<?php
$file_name = $_GET['name'];
$file_delete = dirname(__FILE__, 2) . '\\u\\' . $file_name;
unlink($file_delete);
?>
$file_delete here is the absolute path to the file you want to delete.
Reminder: / is used for Unix systems, \ for Windows.
PHP doc:
- http://php.net/manual/en/function.unlink.php
- http://php.net/manual/en/function.dirname.php
I also had same issue with my code. What I did to solve the issue is:
First execute:
var_dump($image_variable) // var_dump($file_delete) in your case.
It outputs: string(23)(my-image-path )
When I started counting string I just found 22 characters. I wondered where is the 23rd?
I checked and count carefully, at the end I found that there is space at the end of my image path. So I used php trim() function to remove white spaces. Like,
$trimed_path = trim($image_variable) // trim($file_delete) in your case.
Second: Now execute,
unlink($trimed_path).
OR CHECK LIKE
if(unlink($trimed_path))
{
echo "File Deleted";
}
else
{
echo "Error Deleting File";
}
Took me a couple of hours to figure out. As mentioned above unlink() is picky when it comes to paths.
Solution is:
1st) Define the path (this is how Wordpress does it btw):
define( 'ROOTPATH', dirname(dirname(__FILE__)) . '/' );
2) Do:
unlink(ROOTPATH.'public_html/file.jpg');

scandir in php with special (hungarian) characters

I've been trying to read (image) files a folder in my website. But, there's a problem! There are some special hungarian character (like: á,í,ú,ű,ó,é, etc...) in the name of the folder which I want to read. I've been trying many mode, but it doens't work. My php code:
<?php
$images = scandir("images/2005/avatás/");
foreach($images as $file) {
print($file);
}
?>
I've been trying to fix the problem, with mb_convert_encoding(), urlencode(), urldecode() functions, but these couldn't help me.
Have you got any idea?
Thanks!
iconv("windows-1254", 'UTF-8', $foldername)
Same problem same language, I assume this should work for you too
You could try using iconv()
<?php
$folder = iconv("windows-1251", "UTF-8", "images/2005/avatás/");
$images = scandir($folder);
foreach($images as $file) {
print($file);
}

PHP Upload Script Not Functioning

The following code appears to upload ZIPs, but does not in fact create the document according to the naming convention I'm using:
// Upload ZIP
if((isset($_FILES['arquivo']['name'][0])) && ($_FILES['arquivo']['type'] == 'application/zip, application/octet-stream') && ($_FILES['arquivo']['size'] < 1000000)){
$arq = 'documento_'.uniqid().".zip';
$path = '/home/domain_name_here/www/documentos/';
$documento = $path.$arq;
move_uploaded_file($_FILES['arquivo']['tmp_name'], $documento);
chmod($documento, 0777);
}
Not entirely sure why this isn't functioning the way it should. Seems like the files upload to temporary and fail somewhere between there and the documentos folder.
Thanks in advance!
I would suspect that this line contains an error:
$arq = 'documento_'.uniqid().".zip';
You have double quote instead single quote wrapping .zip. It should be:
$arq = 'documento_'.uniqid().'.zip';

Strstr return false instead of true

I want to open file, check if string deosn't exist in file write it.
Doing this:
$fp=fopen('categories.txt','a+');
$content=fread($fp,filesize('categories.txt'));
if(!strstr($content,$cat)){
fwrite($fp,','.$cat);
}
fclose($fp);
But I got repeating values in categories.txt after writing.
Only issue I can expect is encoding problem, but all files are utf-8 and in categories.txt I just have latin symbols and few symbols.
Any ideas where is the problem?
try like this.
$pos = strpos($content, $cat);
if($pos === false){
fwrite($fp,','.$cat);
}
Ok, I think problem in fopen. I changed to this, and code starts to work:
$content=file_get_contents('categories.dat');
$type=(string) $type;
$content=(string)$content;
if(!strstr($content,$type)){
file_put_contents('categories.dat',$content.','.$type);
}
Thanks for helps.

Categories