Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I got a problem with my code. It's returning false. I have searched and tried some similar questions here but none of them helped. anyway, im using cPanel here. and i'm sure that the file really exists and so with the folder names. Hope you can help me with this. thanks in advance.
<?php
$filename = 'event-01.jpg';
if ( file_exists( $_SERVER{'/home2/user/public_html'} . "/MyProject/events/event-01.jpg")) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
i have also tried
$filename = 'event-01';
if (!file_exists('http://mysite.com/MyProject/events/event-01.jpg')) {
echo "The file $filename doesn't exist";
}
Problem
To put it bluntly; this...
file_exists( $_SERVER{'/home2/user/public_html'} . "/MyProject/events/event-01.jpg")
is not php.
Solution
Check a file exists
Assuming that the file path is actually:
/home2/user/public_html/MyProject/events/event-01.jpg
Then you should just be using that in file_exists:
file_exists("/home2/user/public_html/MyProject/events/event-01.jpg")
Server root
I assume that what you actually meant to do was:
file_exists($_SERVER['DOCUMENT_ROOT']."/MyProject/events/event-01.jpg")
You might also like to try to var_dump($_SERVER) to see all of the information that it stores.
References
file_exists: http://php.net/file_exists
$_SERVER: http://php.net/manual/en/reserved.variables.server.php
In PHP, $_SERVER is a superglobal containing various server related information and unrelated to what you're trying to achieve.
Simply try this instead:
if (file_exists("/home2/user/public_html/MyProject/events/event-01.jpg")) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Here is my email send php file , email sent but attachment is not working. File is there on that path . Issue is I need to get file name from a linux command and store in a variable . When I called that variable attachment is not working.
<?php
require_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$file_name1 = shell_exec('sed "1q;d" /tmp/file.txt');
$file = "/home/user/.$file_name1";
echo $file;
//set Address data
$mail->Subject = "Acknowledgement Files ".date("m-d-Y:h:i:s")." ";
$mail->MsgHTML(" Acknowledgement Files are here attached");
$mail->AddAttachment($file)
if($mail->Send()) {
echo "Message sent!";
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
If I am going to use full path like
$file = "/home/user/filetxt"
this works but I want to call variable and echo is display correct name.
any thoughts?
Put your variable outside the quotation, like this:
$file = "/home/user/".$file_name1;
So I found issue and solution as well. So when we run any command on linux likels /path/ output is result\n. It adds new line charactor as well. So I changed this line
$file_name1 = shell_exec('sed "1q;d" /tmp/file.txt');
to
$file_name1 = trim(shell_exec('sed "1q;d" /tmp/file.txt'));
to remove new line . AND now it works
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a problem while trying to open a file and display its contents using php
My file called hello.txt
Here is my PHP code
<?php
$filename = 'hello.txt';
$filePath = 'c:\\Users\\bheng\\Desktop\\'.$filename;
if(file_exists($filePath)) {
echo "File Found.";
$handle = fopen($filePath, "rb");
$fileContents = fread($handle, filesize($filePath));
fclose($handle);
if(!empty($fileContents)) {
echo "<pre>".$fileContents."</pre>";
}
}
else {
echo "File Not Found.";
}
?>
I got this from
http://php.net/manual/en/function.fread.php
I keep getting error:
fread(): Length parameter must be greater than 0
Can someone help me please?
Although there are good answers here about using file_get_contents() instead, I'll try to explain wht this is not actually working, and how to make it work without changing the method.
filesize() function uses cache. You probably executed this code while still having the file empty.
Use the clearstatcache function each time the file change, or before testing its size :
clearstatcache();
$fileContents = fread($handle, filesize($filePath));
Also obviously make sure that your file is not empty ! Test it :
clearstatcache();
if(file_exists($filePath) && filesize($filePath)) {
// code
}
It needn't be that hard, and it certainly doesn't require you to read a file in binary mode:
if (file_exists($filePath))//call realpath on $filePath BTW
{
echo '<pre>', file_get_contents($filePath), '</pre>';
}
All in all, you really don't want to be doing this kind of stuff too much, though
If you need to read the entire file's content, there is a shortcut function
http://php.net/manual/en/function.file-get-contents.php
So you don't need to bother creating a file handler and closing it afterwards.
$fileContents = file_get_contents($filePath);
using file_get_contents method of php
echo file_get_contents("text.txt");
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to copy some files from one directory on server to another, but it does not work.
Here is my code:
system('cp /var/www/site1/images/' . $row['imageUrl']. ' /var/www/site2/content/upload/content/item/mid/' . $row['imageUrl']);
$file = '/var/www/site1/images/'.$row['imageUpl'];
$newfile = '/var/www/site2/content/upload/content/item/mid/'.$row['imageUpl'];
if (copy($file, $newfile)) {
echo "success";
}
else
{
echo "failed";
}
Look at the copy() function here: http://php.net/manual/ru/function.copy.php
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am using a cross domain request.A request has been made to php file in a server via ajax from another server.From php side Here, I need to create a file and write some contents in that file.My request is reaching perfectly.But I am not able to create a file.Please help
NOTE : ITS A CROSS DOMAIN REQUEST
<?php
$filename = "lin.txt";
$data2 = "lin IS HERE";
$newFile= fopen($filename, 'w');
chmod($newFile, 777);
fwrite($newFile, $data2);
fclose($newFile);
?>
Thanks in advance
In am not sure what are you going to do, because you haven't told us your code. I think this will help you:
<?php
$file = 'demo.txt';
$content = "Example\n";
file_put_contents($file, $content);
?>
The file is demo.txt (it is at the same location with the PHP document.
$content is the text which will be put on your txt file.
Hope it helps!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I've searched around google for a way to fopen and read a file with an unknown/random name in a specific folder with a specific extension (it will be the only file in that folder, but it gets updated every hour with a new name), but couldn't find anything. Previously the file had the same name every time unfortunately now it changes...
previously i had to only define it this way:
$file = "/var/uploads/quarter.csv";
$handle = #fopen($file, "r");
but with a random name it doesn't work anymore.
so, I've tried:
function the_file($dir = '/var/uploads/') {
$files = glob($dir . '/*.csv');
$file = $files;
}
$handle = #fopen($file, "r");
script continues...
but it doesn't work. Any help would be appreciated, thanks.
The $file variable doesn't exist outside the function scope and hence you won't be able to use it outside the function. Also, glob() returns an array -- if there is only one file, you can just get the first element of the array and return it, like so:
function the_file($dir = '/var/uploads/') {
$files = glob($dir . '/*.csv');
return $files[0]; // return the first filename
}
Now to store it in a variable, you can do:
$file = the_file(); // or specify a directory
# code ...
A possible solution can be:
If there is only one file in the directory, you can do a scandir to get the contents of the folder. (see PHP ref.)
$dir = 'your_folder_name';
$files1 = scandir($dir);
The content of the $files will be:
Array
(
[0] => .
[1] => ..
[2] => yourfile.csv
)
So you can get the full name of the file with:
$filename = $files1[2];