Check exist file with php - php

I want to check exist file with php or yii2
filesize and is_file and file_exists are not working !
filesize error is : filesize(): stat failed for....
is_file and file_exists return false !
This is my code :
$fileI = $urlBase . "$home/../siteImage/$logo";
$fileI is :
http://localhost/iicitySite/web/index.php/../siteImage/parvaz.png
$home is :
$home -> /iicitySite/web/index.php
This is has correct image in view :
echo "
<div class='col-lg-12 col-xs-12 col-sm-12 col-md-12 forsatItem'>
<div class='backItem'>
<div class='rightM col-lg-4'>
<div class='imgForsat'>
<img src='$home/../siteImage/$logo' alt=''/>
</div>
.
.
.
.

Try this
$url = 'http://www.example.com';//or file path
$headers = get_headers($url, 1);
print_r($headers);//$headers[0] u will get the status
Source: link

I have checked and verified below code and it is working fine.
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
If you are not able to do this then the issue with your permission. Try to check file that are in desktop or some other permissible directory.
Hope this helps.

In you path you don't need the index.php but only the path
You can use this to get absolute(whole url including http) url :
echo Url::to('#web/siteImage/parvaz.png', true);
// http://codematrics.com/web/siteImage/parvaz.png
If you want to use relative(relative to current path) path than you can use this :
echo Url::to('#web/siteImage/parvaz.png', true);images/logo.png
// http://codematrics.com/web/siteImage/parvaz.png

If you are checking if file exist into server just you have to give the absolute path, I'm using \Yii::getAlias('#webroot') for get the absolute path and I'm executing this from a Controller.
This is my result into web browser.
Absolute Path:/home/hostinguser/public_html/files/temppdf/benja.pdf
The File /home/hostinguser/public_html/files/temppdf/benja.pdf Exists
$pathFile = \Yii::getAlias('#webroot') .'/files/temppdf/' . 'benja.pdf';
echo "<br>Absolute Path:" . $pathFile;
if (file_exists($pathFile)) {
echo "<br>The File $pathFile Exists";
} else {
echo "<br>The File $pathFile Do Not Exist";
}

$url = 'http://www.example.com'; // or file path
$headers = get_headers($url, 1);
print_r($headers); // $headers[0] will get the status

Related

php move_uploaded_file returns true but file is missing on localhost

I am trying to move one image to new folder move_uploaded_file is returning 1 but the file is missing, I am working on localhost with XAMPP
$name = basename($_FILES['arr']['name'][0]);
move_uploaded_file($_FILES['arr']['tmp_name'][0],"\Images");
$name = basename($_FILES['arr']['name'][0]);
move_uploaded_file($_FILES['arr']['tmp_name'][0],'/images/' . $filename);
You have to add a destination for the file to go, including the filename that you wish to assign to it.
Refer to: http://php.net/manual/en/function.move-uploaded-file.php
You should specify the destination file name
if ( move_uploaded_file($_FILES['arr']['tmp_name'],dirname(__FILE__) . '/images/' . $_FILES['arr']['name'] ) ) {
echo "file uploaded";
} else {
echo "error in uploading";
}

I am trying to download a file from mysql, I saved the file location in the database, now I am trying to make a download link for the user on my page

Unable to create the download link. I am fetching the path saved from database and then try to make a link for it to download, but nothing happens.
Below is my code:
$query_print="SELECT vitae_pi FROM pi WHERE username='t124'";
$query_print_run=mysqli_query($conn,$query_print);
$query_print_recordset=mysqli_fetch_assoc($query_print_run);
$query_print_path=$query_print_recordset['vitae_pi'];
echo ' this is file path '.$query_print_path;
Here I am simply trying to create the download link for user t124, instead of using the current user for testing purposes?
This is hyperlink code:
<?php echo "<a href='".$query_print_path."'>".DOWNLOAD."</a>"; ?>
Any suggestions?
This my move file function:
protected function moveFile($file)
{
$filename = isset($this->newName) ? $this->newName : $file['name'];
//echo $filename;
$success = move_uploaded_file($file['tmp_name'], $this->destination . $filename);
if ($success) {
$result = $file['name'] . ' was uploaded successfully';
if (!is_null($this->newName)) {
$_SESSION['current_filename']=$this->newName;
echo $_SESSION['current_filename'];
$result .= ', and was renamed ' . $this->newName;
}
else{
$_SESSION['current_filename']=$file['name'];
echo $_SESSION['current_filename'];
}
//$result .= '.';
//echo $this->newName;
$this->messages[] = $result;
} else {
$this->messages[] = 'Could not upload ' . $file['name'];
}
}
Updating the table with file path:
$file_path_variable1= $destination1.$_SESSION['current_filename'];
echo '$file_path_variable1 : '.$file_path_variable1;
$query1="UPDATE proposal SET whitepaper_prop='$file_path_variable1' WHERE userName_prop='$currentuser'";
$result_query1=mysqli_query($conn,$query1);
....................
SOLUTION CODE IS:
Solution code is :
$query_print="SELECT vitae_pi FROM pi WHERE username='t115'";
$query_print_run=mysqli_query($conn,$query_print);
$query_print_recordset=mysqli_fetch_assoc($query_print_run);
$query_print_path=$query_print_recordset['vitae_pi'];
$dir= 'uploaded/';
$path=opendir($dir);
<?php
}while($query_pi_array=mysqli_fetch_assoc($query_pi_result));?>
<div>
<?php while($file=readdir($path)){
if($file != "." || $file !=".."){
if($file==$query_print_path){ ?>
Proposal Whitepaper
What does this display ?
<?php echo "<a href='".$query_print_path."'>".DOWNLOAD."</a>"; ?>
DOWNLOAD should be part of the PHP string, if not, it will be considered as a constant :
<?php echo "<a href='".$query_print_path."'>DOWNLOAD</a>"; ?>
Also, use double quotes for HTML attributes :
<?php echo "DOWNLOAD"; ?>
And the optimized way (to avoid useless string parsing) :
<?php echo 'DOWNLOAD'; ?>

file download in yii shows file not exist

I am trying to write a script in Yii for downloading files. view file isecho CHtml::link('Major Oilseeds: World Supply and Distribution.','downloadpdf', array('class'=>'btn btn-darkorange')); and the controller code is $path = Yii::app()->request->hostInfo . Yii::app()->request->baseURL . '/download/Major_Oilseeds.csv';
echo $path;
if(file_exists($path)){
Yii::app()->getRequest()->sendFile( 'Major_Oilseeds.csv' , file_get_contents($path) );
}
else{
echo '<br>File not found';
} the code echo $path dispalys the location as http://localhost/projectv2/download/Major_Oilseeds.csv and the download folder contains the file named "Major_Oilseeds.csv" but its always showing "File Not Found" Error. plz somebody help me to solve this. I have also tried the code $path = Yii::app()->request->hostInfo . Yii::app()->request->baseURL . '/download/Major_Oilseeds.csv';
// $filename = 'Major_Oilseeds.csv';
header('Content-Disposition: attachment; charset=UTF-8; filename="'.$path.'"');
$utf8_content = mb_convert_encoding($content, "SJIS", "UTF-8");
echo $utf8_content;
Yii::app()->end();
return; but its also not working :-(
file_exists can only work with local files. In your case you are trying to use file_exists with a URL.
You can find a workaround here: http://uk1.php.net/file_exists (please search for "http" in this page)

How to use Unlink() function

I'm trying to use PHP unlink() function to delete away the specific document in the folder. That particular folder has already been assigned to full rights to the IIS user.
Code:
$Path = './doc/stuffs/sample.docx';
if (unlink($Path)) {
echo "success";
} else {
echo "fail";
}
It keep return fail. The sample.docx does reside on that particular path. Kindly advise.
I found this information in the comments of the function unlink()
Under Windows System and Apache, denied access to file is an usual
error to unlink file. To delete file you must to change the file's owner.
An example:
chown($tempDirectory . '/' . $fileName, 666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody"
unlink($tempDirectory . '/' . $fileName);
So try something like this:
$path = './doc/stuffs/sample.docx';
chown($path, 666);
if (unlink($path)) {
echo 'success';
} else {
echo 'fail';
}
EDIT 1
Try to use this in the path:
$path = '.'
. DIRECTORY_SEPARATOR . 'doc'
. DIRECTORY_SEPARATOR . 'stuffs'
. DIRECTORY_SEPARATOR . 'sample.docx';
Try this:
$Path = './doc/stuffs/sample.docx';
if (file_exists($Path)){
if (unlink($Path)) {
echo "success";
} else {
echo "fail";
}
} else {
echo "file does not exist";
}
If you get file does not exist, you have the wrong path. If not, it may be a permissions issue.
This should work once you are done with the permission issue. Also try
ini_set('display_errors', 'On');
That will tell you whats wrong
define("BASE_URL", DIRECTORY_SEPARATOR . "book" . DIRECTORY_SEPARATOR);
define("ROOT_PATH", $_SERVER['DOCUMENT_ROOT'] . BASE_URL);
$path = "doc/stuffs/sample.docx";
if (unlink(ROOT_PATH . $Path)) {
echo "success";
} else {
echo "fail";
}
// http://localhost/book/doc/stuffs/sample.docx
// C:/xampp/htdocs\book\doc/stuffs/sample.docx
You need the full file path to the file of interest. For example: C:\doc\stuff\sample.docx. Try using __DIR__ or __FILE__ to get your relative file position so you can navigate to the file of interest.

ftp_put not transferring files to ftp

I have the following code below, I cannot get why it's not working.
$server=("ftp.blah.com");
$connect=ftp_connect($server);
$dest='/';
$login_result=ftp_login($connect,"blah#blah.com","lol");
if(!($login_result)||!($connect))
{
$error;
} else {
echo "success";
}
$file= 'Tiny-' . $time. '.txt';
$upload=ftp_put($connect,$dest,$file,FTP_ASCII);
if (!$upload)
{
echo "failed to upload";
} else{
echo "successfully uploaded";
}
ftp_close($connect);
When i run the code, i get the error "Warning: ftp_put(Tiny-201201070758.txt): failed to open stream: No such file or directory
I have made the destination folder of the ftp write and read accessible.
I have also tried to include the full path of the text file by:
$file= 'C:\xampp\htdocs\Tiny-' . $time. '.txt'
or
$file= 'C:\\xampp\\htdocs\\Tiny-' . $time. '.txt'
I also tried using FTP_Binary instead of ASCII, still no luck.
nothing works.
Have you got url_fopen active on your server? Check with phpinfo().

Categories