I would like to check that a file uploaded to my OpenShift app has a text extension (.txt or .tab). Following some advice given here I wrote the following code, with echoes added to help debug:
$AllowedExts = array('txt','tab');
echo "AllowedExts: " . $AllowedExts[0] . " and " . $AllowedExts[1] . "<br>";
$ThisPath = $_FILES['uploadedfile']['tmp_name'];
echo "ThisPath: " . $ThisPath . "<br>";
$ThisExt = pathinfo($ThisPath, PATHINFO_EXTENSION);
echo "ThisExt: " . $ThisExt . "<br>";
if(!in_array($ThisExt,$AllowedExts) ) {
$error = 'Uploaded file must end in .txt or .tab';
}
echo "error echo: " . $error . "<br>";
On uploading any file, the echoed response was:
AllowedExts: txt and tab
ThisPath: /var/lib/openshift/************/php/tmp/phpSmk2Ew
ThisExt:
error echo: Uploaded file must end in .txt or .tab
Does this mean that OpenShift is renaming the file upon upload? How do I get the original filename and then check its suffix? More generally, is there a better way to check the file type?
$_FILES['uploadedfile']['tmp_name'] contains the name of a temporary file on the server (which can be moved with move_uploaded_file()). If you want to check the original name of the uploaded file on the client machine use $_FILES['uploadedfile']['name'].
That's not an Open Shift issue, it's the standard way of PHP.
For further details see http://php.net/manual/en/features.file-upload.post-method.php
For other ways to detect the file type see http://php.net/manual/en/ref.fileinfo.php
Related
So, I am working on a system where people working in a company can upload files to a system which will be sorted by departments. I have managed to get the files info(name, size, type), but the problem occurs while trying to upload the file.
I get:
move_uploaded_file(/Advanced Java Programming.pdf): failed to open stream: Permission denied
move_uploaded_file(): Unable to move 'C:\xampp\tmp\php1B99.tmp' to '/Advanced Java Programming.pdf'
I have set the permissions of the folder where the file needs to be uploaded to everybody (777).
Here's my code
<?php
$department = $_POST['department'];
$file = $_FILES['fileToUpload'];
echo "<b>Department: </b>" . $department . "<br>";
echo "<b>Name: </b>" . $file['name']. "<br>";
echo "<b>Size: </b>" . $file['size'] . " bytes<br>";
echo "<b>Type: </b>" . $file['type'];
move_uploaded_file($file['tmp_name'], "/". $file['name']);
?>
Try using an absolute path for your destination or at least start it with the DIR-constant, "/" ist not a valid (Windows)-path.
Also think about using the constant DIRECTORY_SEPARATOR as "/" is a *nix-standard, but as you're running on Windows, it should be "\" - using the constant will hold the right slash for every system.
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 developing a system where the user may upload up to four photos. If the user does not upload a photo I would like that the follow text will be displayed: 'empty'.
I have prepared the below code however I didn't managed :/ The problem is that when an image is uploaded it still prints 'empty' and the uploaded image does not show up.
<?php if (file_exists('../files/collection/photo2/' .$collection['Collection']['photo_dir2'] . '/thumb_' . $collection['Collection']['photo2']))
{
echo $this->Html->image('../files/collection/photo2/' . $collection['Collection']['photo_dir2'] . '/thumb_' . $collection['Collection']['photo2']);}
else
{
echo ('empty');
}
?>
I appreciate you guidance and help :)
TLDR: The file you're checking for doesn't exist. Fix your path.
Detail:
You're trying to use the same path for both your HTML <img> as well as the PHP file_exists() check.
The problem is, that the HTML image is looking for a file via the user's browser, where the file_exists() method is looking for the file via your server. The two paths are very rarely the same.
Try using a correct path in your PHP's file_exists() method, and it should pass the check.
For example:
if(file_exists(APP . 'files' . DS . 'collection' . DS . 'photos2' . $collection['Collection']['photo_dir2'] . DS . 'thumb_' . $collection['Collection']['photo2'])) {
echo $this->Html->image('../files/collection/photo2/' . $collection['Collection']['photo_dir2'] . '/thumb_' . $collection['Collection']['photo2']);
}
else {
echo ('empty');
}
Can I get an eyeball on my symlink?
I'm trying to download a file from one directory, while the file actually exists in another.
I've got the actual file, and the symlink in seperate subdirectories, but both reside in the public html(both are web accessible).
I've verified the file and file location on my (shared Linux) server by going to the file directly.
The link is being created (I've used readlink, is_link, and linkinfo), and I can see it when I FTP in.
I believe I am probably just having a misunderstanding of the directory structure.
I put the file here: ./testdownload/
I put the symlink here: ./testDelivery/
<?php
$fileName = "testfiledownload.zip";//Name of File
$fileRepository = "./testdownload/";//Where the actual file lives
$downloadDirectory = "./testDelivery/";//Where the symlink lives
unlink($downloadDirectory . $fileName); // Deletes any previously exsisting symlink (required)
symlink($fileRepository . $fileName, $downloadDirectory . $fileName);
$checkLink = ($downloadDirectory . $fileName);
if (is_link($checkLink))
{
echo ("<br>Symlink reads: " .readlink($checkLink) . "<br>");
echo ("<br>LinkeInfo reads: " . linkinfo($checkLink));
}
?>
<p><a href="<?php echo ("/testDelivery/" . $fileName); ?>"</a>SymLink</p>
<p><a href="<?php echo ("/testdownload/" . $fileName); ?>"</a>regular link</p>
Everything looks right to me....but the link won't work.
Help?
Ultimately, I will put the source data outside the public area...this is just for testing.
(I'm trying to find a better solution for download than chunking out fread which fails for poor connections. (200-400MB files))
My problem (appears) to be not providing the absolute path for the symlink.
I've added the absolute path below to the same code above, to give a working copy:
<?php
$absolutepath = ( $_SERVER['DOCUMENT_ROOT']);
$fileName = "testfiledownload.zip";//Name of File
$fileRepository = "/testdownload/";//Where the actual file lives
$downloadDirectory = "/testDelivery/";//Where the symlink lives
unlink($absolutepath .$downloadDirectory . $fileName); // Deletes any previously exsisting symlink (required)
symlink($absolutepath . $fileRepository . $fileName, $absolutepath. $downloadDirectory . $fileName);
$checkLink = ($absolutepath . $downloadDirectory . $fileName);
if (is_link($checkLink))
{
echo ("<br>Symlink reads: " .readlink($checkLink) . "<br>");
echo ("<br>LinkeInfo reads: " . linkinfo($checkLink));
}
?>
<p><a href="<?php echo ("/testDelivery/" . $fileName); ?>"</a>SymLink</p>
<p><a href="<?php echo ("/testdownload/" . $fileName); ?>"</a>regular link</p>
This original post, is a duplicate (though I didn't see it until now)
Create a valid symlink for PHP file
(Most of the answers given for that question were wrong however--but the original poster figured it out, and it worked for me too)
Its been a long time since i used php and i need some help regarding this basic function.
{
$msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
$msg .= " File Size: " . #filesize($_FILES['fileToUpload']['tmp_name']);
#unlink($_FILES['fileToUpload']);
}
Suppose i have a file in the $_FILES['fileToUpload'], how do i create an upload dir if it does not exist, and save the file there?
Thanks!
use is_dir() to check if the directory exitst http://us.php.net/manual/en/function.is-dir.php
then use mkdir() if it doesn't exist http://us.php.net/manual/en/function.mkdir.php