I made a php upload script that would be executed after a form with the upload was submitted.
$game_tmp = $game_file['tmp_name'];
$game_name = $game_file['name'];
$game_dest = "/games/" . game_name;
if (move_uploaded_file($game_tmp, $game_dest)) {
echo "<b>SUCCESS:</b><br />";
}
else {
echo "Error.";
}
It always output Error, so I checked my php.ini file and phpinfo(). my php.ini shows upload_tmp_dir with no value and so does the value in phpinfo(). I changed the calue of the directory to /upload, and changed the open_basedir to have a value of upload_tmp_dir. Yet, when I look into phpinfo(), it still shows upload_tmp_dir with a local and master value of no value. I believe this is the problem that is stopping my upload script from working. I created a /upload folder also and gave its permission value 777. Yet, this problem persists. I am not sure what is causing this problem.
Try this, you must use the $_FILE super global:
if($_FILES["game_file"]["error"] == UPLOAD_ERR_OK) {
$game_tmp = $_FILES['game_file']['tmp_name'];
$game_name = $_FILES['game_file']['name'];
$game_dest = "/games/" . $game_name;
if (move_uploaded_file($game_tmp, $game_dest)) {
echo "<b>SUCCESS:</b><br />";
}
else {
echo "Error.";
}
}
if upload_tmp_dir is not specified, php will use system's default tmp directory. You can use sys_get_temp_dir() function to identify temporary directory being used by PHP.
Have you tried printing $game_file var?
If not, try print_r($game_file) or var_dump($game_file) to view if its actually being set. You can also print $_FILE to see error message (http://php.net/manual/en/features.file-upload.errors.php)
move_uploaded_file could fail for many reason. Also, confirm if the destination directory exists and is writable.
Typos:
$game_dest = "/games/" . game_name;
^---you forgot a $ here
That's an undefined constant, so PHP will "politely" treat it as an unquoted string, and you're generating "/games/game_name" instead.
never EVER do devel/debug work in PHP with display_errors and error_reporting turned off. It's the equivalent of going "lalalalala can't hear you" with your fingers stuffed in your ears.
And you're also simply assuming your upload succeeded. There's a ['error'] parameter in $_FILES for a reason. Check it FIRST, before you start fiddling with a file that may not even be there.
Related
I have a php function that renames two separate image files from a temporary to permanent path after first confirming that the temporary path exists.
When it checks for the fist file it works fine but, for some reason, the second file never passes the if(file_exists()) even though I can confirm with 100% certainty that the file path being checked does, in fact, exist.
The image files have different names but the codes are otherwise structured exactly the same so I can't see why one would work and the other wouldn't.
if(file_exists('temp/'.strtolower($option['image1']))){
$path1 = 'images/'.strtolower($option['image1']); // upload directory
$tmp1 = 'temp/'.strtolower($option['image1']);
if(rename($tmp1, $path1)){
$error = 0;
}else{
$error = 4;
}
}
if(file_exists('temp/'.strtolower($option['image2']))){
$path2 = 'images/'.strtolower($option['image2']); // upload directory
$tmp2 = 'temp/'.strtolower($option['image2']);
if(rename($tmp2, $path2)){
$error = 0;
}else{
$error = 5;
}
}
Is there an issue with calling file_exists() twice? How else can I check for both paths?
Edit
As per Marco-A's suggestion, I added clearstatcache(); between the two if/then blocks and it worked like a charm.
The only two possibilities (if you're absolutely sure the file path exists) I'm seeing are either 1.) a stat cache problem (you can clear the cache with clearstatcache) or 2.) a permission issue. Consider this:
$ touch /tmp/locked/file
$ php is_file_test.php
$ bool(true)
$ chmod -x /tmp/locked
$ php is_file_test.php
$ bool(false)
So it might be, that the parent directory of that file doesn't have the x (executable) permission bit set. This prevents any process from iterating and accessing the directory's content.
The uploaded file names can have uppercase characters. If you use strtolower in the file_exists function, you probably wouldn't be looking for the original file path.
if(file_exists('temp/' . strtolower($option['image']))){
// ...
}
Should be changed to:
if(file_exists('temp/' . $option['image'])){
// ...
}
I'm just having some trouble working with $_FILES and file_get_contents.
$nomFichier = $_FILES['pieceJointe']['name'];
echo is_readable($_FILES['pieceJointe']['tmp_name']);
echo $_FILES['pieceJointe']['error'];
if(file_get_contents($_FILES['pieceJointe']['tmp_name']) == false)
{
echo "impossible de lire le fichier.<br/>";
}
else
{
// store the file into a BLOB field in my database
}
is_readable display "1", $_FILES['pieceJointe']['error'] says 0.
But file_get_contents return false.
I notice that happen only with files which the name contains accents.
Everything is working fine with only letters/numbers file names.
Do I missed something ?
ps: I'm french student, not professional, sorry for my english :o
Thx!
Use var_dump to see the actual value of is_readable(). The "1" that you see is actually the output of $_FILES['pieceJointe']['error'] which means the file you are trying to upload is too large. See http://www.php.net/manual/en/features.file-upload.errors.php.
I guess that there is some conflict of file_get_contents function and open_basedir directive of your php.ini file:
http://www.php.net/manual/en/ini.core.php#ini.open-basedir
Try to use move_uploaded_file function before you read the file.
It will move uploaded file from temporary directory of your server to specified directory, (open_basedir directive only effects the destination parameter of move_uploaded_file)
Isn't $_FILES['pieceJointe']['tmp_name'] just a file name name, instead of a valid full path?
Maybe move the file first with move_uploaded_file before opening it.
When I use file_get_contents on a path like /a/path/to/a/../file.php, it gets the content just fine. If I call file_exists first (or is_file or realpath), the return values indicate that the file does not exist. What seems to be the issue?
Edit: Here is some additional information condensed from comments to answers:
I am running Mac OS X 10.9 with php 5.5.6, so safe mode should not be an issue (it was removed in version 5.4)
I tried clearing the file cash by calling clearstatcache(true, $dir1)
The file in question is 362 bytes in size, but I reproduced this issue with several different files in a medley of locations.
open_basedir is commented out in the php.ini
The file is local (the first file I tried was in the same directory as the script)
The issue exists in the command line (phpUnit) and in the browser.
The permissions on the file in questions are -rwxrwxrwx (I sudo-chmod-777ed the file)
This is a code snippet that creates the behavior:
$dir1 = '/a/path/to/a/../file.php';
$dir2 = '/a/path/to/file.php';
echo "File content dir1:\n";
echo file_get_contents($dir1);
echo "\ndir1 exists: ".(int)file_exists($dir1);
echo "\n\nFile content dir2:\n";
echo file_get_contents($dir2);
echo "\ndir2 exists: ".(int)file_exists($dir2);
the output is:
File content dir1:
The actual content of the file. I promise!
dir1 exists: 0
File content dir2:
The actual content of the file. I promise!
dir2 exists: 1
It sounds like you have safe mode turned on and are attempting to access a file that PHP would consider unsafe when running in safe mode. From the manual:
Warning
This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.
EDIT: You can also reproduce this behavior if /a/path/to/a/ is not a real path. For example:
<?php
$dir1 = '/realDir/realDir2/filetoinclude.php';
echo "File content dir1:\n";
echo file_get_contents($dir1); // outputs file contents
echo "\ndir1 exists: ".(int)file_exists($dir1); // outputs 1
$dir2 = '/realDir/realDir2/realDir3/../filetoinclude.php';
echo "\n\nFile content dir2:\n";
echo file_get_contents($dir2); // outputs file contents
echo "\ndir2 exists: ".(int)file_exists($dir2); // outputs 1
$dir3 = '/realDir/realDir2/NotARealDirectory/../filetoinclude.php';
echo "\n\nFile content dir3:\n";
echo file_get_contents($dir3); // outputs file contents
echo "\ndir3 exists: ".(int)file_exists($dir3); // outputs 0
This is because file_exists needs to traverse the entire path, literally, so it looks for the missing directory and fails. I'm not sure exactly what file_get_contents does that is different, and I can't find much on Google, but it clearly does some parsing of the path that is different from what file_exists does.
I am providing the workaround that I developed with a regex, if others have this same issue. I hate to be using this hack, and I still don't understand why I am having this issue, but hopefully someone will come up with an actual solution.
Before calling file_exists I now call this function:
function resolve($path) {
$regex = "/(.?)(\/[^\/]*\/\.\.)(.*)/";
$result = preg_replace($regex, "$1$3", $path);
if ($result != $path) {
$result = resolve($result);
}
return $result;
}
Hi there is there any way to check if .exe file exists on a given path or not.
I have installation of ImageMagic. I have a path of convert.exe of Image Magic. I need to check that in given path the convert.exe exists or not. I have implemented
$settingFileContent = file_get_contents($settingFilePath);
// print_r($settingFileContent);
$allPaths = unserialize(stripslashes($settingFileContent));
if (isset($allPaths['IMAGE_CONVERT_EXE'])) {
//cho $allPaths['IMAGE_CONVERT_EXE'];
if (file_exists($allPaths['IMAGE_CONVERT_EXE'])) {
$analysisResultObj->level = ENUM_SUCCESS;
} else {
$analysisResultObj->level = ENUM_ERROR;
$analysisResultObj->infoText = "Image Magic convert.ext has wrong path";
Logger::getLogger('Application')->error('Image Magic convert.ext has wrong path');
}
}
I can change the value of $allPaths['IMAGE_CONVERT_EXE'] in file. When I change to wrong value even in that condition it returns true.
Based on the documentation comment specifically about PHP on Windows I'm guessing (and let's be clear: everything in PHP is a guess) try this:
$file = 'd:/somfolder/imagemagic/convert.ext'
if(file_exists($file)) {
// should be false
}
Based on your actual code have you tried:
$file = $allPaths['IMAGE_CONVERT_EXE'];
if(file_exists($file)) {
// should be false
}
Looking at the documentation someone commented about having this same problem on Windows and being unable to return the correct result when concatenating string values. While you are not concatenating string values together its at least worth a shot to make sure there isn't something else strange going on.
To me it sounds like you're trying to get wether or not the Imagemagick extension exists. PHP provides ways for doing just that thus eliminating your extrapolated and insane approach all together.
<?php
echo extension_loaded('imagick');
?>
Additionally, you can get an idea of your installed extensions via
<?php
print_r(get_loaded_extensions());
?>
I am trying to open a file but for some reason I cannot even though the file is there and even has 777 permission. The code is the following:
$fileatt = "/opt/lampp/htdocs/a.pdf";
echo "File size is ".filesize($fileatt)."<br>";
if (file_exists($fileatt)) {
echo "The file ".$fileatt." exist <br>";
$file = fopen($fileatt, ‘rb’);
if ($file == false) {
echo "Could not open the file !";
}
} else {
echo "The file ".$fileatt." does NOT exist <br>";
}
The result is:
File size is 1252121
The file /opt/lampp/htdocs/a.pdf exist
Could not open the file !
Why can't I open the file ? Where is my mistake ?
Thanks
Where is my mistake ?
You don't have error reporting properly set. there are 2 things to remember.
error reporting level. set by error_reporting ini directive or error_reporting() function. should ALWAYS be at E_ALL or higher.
error messages destination.
on a development server it should be display
on a live server it should be a log file
Thus, for a quick solution, put these 2 lines at the top of your script
error_reporting(E_ALL);
ini_set('display_errors',1);
but later set up these settings as a permanent ones for the whole site (according to the server state)
once you done it, you will have an answer to your question.
Note that you will have not a mere guess from the fellow stackoverflowers, but exact explanation of the matter from the system itself.