Zend File Path Validation - php

In a zend framework application I want to validate a path to an image file. The file may be a previously uploaded file that reside in a folder inside the server machine or accessed via an url. I want to check weather the file path is valid or not. I don't want to use zend file element and upload the file. I have searched many times and was not successful. Could anyone tell me how to do that?

You are probably looking for the PHP file_exists function.
Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
You could probably build a validator around that, or just use the function directly.
For files on remote servers you will probably need to use something like file_get_contents which also returns false if the file doesn't exist.

Related

Can I define a folder where to download to, using force_download()?

I'd need to define a folder where a downloaded files is placed.
Is it possible to achieve a download into a specific folder using the force_download() function, of Codeigniter's framework?
force_download() is part of CI download helper
Generates server headers which force data to be downloaded to your
desktop. Useful with file downloads. The first parameter is the name
you want the downloaded file to be named, the second parameter is the
file data.
that said, a file will be downloaded to your designated download folder, wherever that is on your local disk. You can use this approach to make files downloadable for any user
what you are looking for is to use the CI FTP Class:
Downloads a file from your server. You must supply the remote path and
the local path, and you can optionally set the mode. Example:
$this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
you must make sure that each time you call this to have the user supplying you with a valid local path, where the downloaded files will be stored.

function file_exists not working in php

I have been trying to find if a file_exist in the directory. If not i want to use a different image. But as i am using the file_exists function it always returns false.
The code i used is
while($r=mysql_fetch_row($res))
{
if(!file_exists('http://localhost/dropbox/lib/admin/'.$r[5]))
{
$file='http://localhost/dropbox/lib/admin/images/noimage.gif';
}
else
$file='http://localhost/dropbox/lib/admin/'.$r[5];}
But the function always return false even if the file exits. I checked that by using
<img src="<?php echo 'http://localhost/dropbox/lib/admin/'.$r[5]; ?>" />
This displayed the image correctly.
Please Someone Help Me
file_exists uses file system paths, not URLs. You use URLs in a browser to access your PHP scripts through a web browser and web server over the network. The PHP script itself can access the local file system though and uses that, it does not go through the network stack to access files.
So use something like file_exists('C:\\foo\\bar\\dropbox\\lib\\admin\\' ...).
You are passing URL to the file_exists function which is wrong. Instead of that pass your local path of the folder.
while($r=mysql_fetch_row($res))
{
if(!file_exists('/dropbox/lib/admin/'.$r[5]))
{
$file='/dropbox/lib/admin/images/noimage.gif';
}
else
$file='/dropbox/lib/admin/'.$r[5];
}
file_exists does not support addresses using HTTP (you can see this because stat is not included on the list of wrappers supported over HTTP). As the file_exists documentation says, remote files can be checked with some wrappers, such as FTP, but it is not possible over HTTP, so file_exists will always return false.
Presuming that this file is on the local machine (which is suggested by localhost, you'll need to access it with a local file path. It's hard to guess what this might be for you, but it might look like /var/www/dropbox....
file_exists() checks if file exists in local filesystem. You're passing an URL. Change it to local path to your dropbox directory and it should work:
if(file_exists('/path/to/your/dropbox'))
You are passing URL to the file_exists function which is wrong parameter. Instead of that pass your local path there.
To know more about file_exist() function read this php manual :
http://php.net/manual/en/function.file-exists.php
The function file_exists can only work for URL protocols that are supported by the stat() function in PHP.
Currently, the http protocol is not supported by this wrapper.
http://uk3.php.net/manual/en/wrappers.http.php

PHP receive file as byte array using Zend_Form

We are using Zend_Form inside a PHP application for building an input file html element. We can set the 'destination' of this element, and when calling receive() the file will be saved to the specified location.
We want to be able not to save the file to disc at all, but grab the file as a byte array and do something else with it.
Is this possible? If it is not possible with Zend_Form(), can it be done any other way?
EDIT: The reason why we cannot write to disc is because the application runs on Azure, and it seems that it does not have write access rights anywhere, not even in the temp folder. We get an exception from Zend saying that 'The given destination is not writeable'.
The only thing that seems viable would be to save the file using the php://memory protocol.
I've never had reason to implement but it looks a simple as setting the save location of the file to php://memory here is the link to the manual page PHP I/O Wrappers.
All PHP uploads are written to the file system regardless of using Zend or not (see upload_tmp_dir and POST method uploads).
Files will, by default be stored in the server's default temporary
directory, unless another location has been given with the
upload_tmp_dir directive in php.ini.
Instead of using receive to process the upload, try accessing it directly using the $_FILES array which would let you read the file into a string using file_get_contents() or similar functions. You can however, still use Zend_Form to create and handle the form in general.
You could set up shared memory upload_tmp_dir to map a filesystem to memory where uploaded files are held. Be cautious with this as if someone attempts to upload a very large file, it will go into memory which could affect performance or your cost of service.
Ultimately, Zend_File_Transfer_Adapter_Http::receive() calls move_uploaded_file() to move the file from its temporary location to the permanent location. In addition it makes sure the upload is valid and filters it, and marks it as received so it cannot be moved again (as that would fail).

How do I make move_uploaded_files work in PHP?

What I have right now for file upload is:
move_uploaded_file($filetemp, "files/$filename");
With filetemp referring to $_FILES['fileupload']['tmp_name'], filename referring to $_FILES['fileupload']['name'], and files referring to a folder of that name inside the folder where the PHP file is.
However, this does not move the file to the files folder. How do I make it so that the function moves the file there?
Thanks!
Nerd With a Vengeance
There could be any number of reasons why this might not be working.
The first thing to check is permissions - make sure the webserver has write permissions to the directory you're trying to write to.
Also, turn your error reporting up - see what warnings are being generated on failure (assuming that you're return value is indeed false).

php uploading file?

I just want to know that if I am using move_uploaded_file function and use two argument first as the name of file and second as the destination.
Normally I have uploaded many files with class uploader but now I want to give the destination as http://www.example.com/testing/
Although I have given 777 permission to this folder but when I try to execute the upload code error came
Destination directory can't be created. Can't carry on a process.
How can I upload the file local to server using php code?
If you are passing http://www.mydomain.com/testing/ as the target, this is wrong.
You can't just upload files to servers via HTTP, you only can do that to local folders, can you paste the exact code so we can know better what are you trying to do?
move_uploaded_file is a server-side function, so all the paths should be specified server side.
If your upload.php (i'm assuming the filename) is in the main directory of the website www.mydomain.com/ which is probably /home/youruser/public_html/ then you can specify the destination as simply "testing/"
If your upload file is in some nested directory, then it may work better to specify the full destination path:
/home/youruser/public_html/testing
good luck

Categories