Imagejpeg returns false but file exists - php

Yesterday I got an output imagejpg() (albeit unreadable character because I havent set the header) but today nothing. The only thing I can that I changed is enabling special permissions on the server. Any idea what else could have affected it? The output for the following code is:
The file exists
Imagejpeg: FALSE
Imagejpeg: FALSE
The file is writable
The output is the same if $filename = $URL.
The code:
clearstatcache();
$filename = $_SERVER['DOCUMENT_ROOT'] . "/wordpress/wp-content/themes/mytheme/images/thumbnails/sb1778/1.jpg";
$URL = get_template_directory_uri() . "/images/thumbnails/sb1778/1.jpg";
if (file_exists($filename)) {
echo "<BR> The file exists";
} else {
echo "<BR> The file does NOT exist";
}
if((imagejpeg($filename)) === true)
{echo "<BR> Imagejpeg: TRUE";}
else {echo "<BR> Imagejpeg: FALSE";}
imagejpeg($filename);
if((imagejpeg($URL)) === true)
{echo "<BR> Imagejpeg: TRUE";}
else {echo "<BR> Imagejpeg: FALSE";}
imagejpeg($URL);
if (is_writable($filename )) {
echo '<BR> The file is writable';
} else { echo '<BR> The file is NOT writable';
}

The image needs to go through the Imagescreatefromjpeg function first:
$filename = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'] . "/wordpress/wp-content/themes/mytheme/images/thumbnails/sb1778/1.jpg");

Related

PHP move_uploaded_file is not working

I am not sure what is wrong here but I know it has to do with move_uploaded_file. I have tried changing the name that the file gets changed to and I have changed the chmod of the directory to 777. From what I can tell there is no reason why the file isn't getting moved, but it isn't.
Here is the code in question. Keep in mind this is part of a Wordpress plugin, so I use functions from there in my code.
if ( isset($_FILES["file"])) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"];
die;
} else {
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
die;
} elseif(strtolower(end(explode('.', $_FILES['file']['name']))) != "csv") {
echo "File is not a .csv";
die;
} else {
if(move_uploaded_file($_FILES["file"]["tmp_name"], plugin_dir_path( __FILE__ ) . "uploads/uploaded_file.csv")) {
echo "Stored in: " . plugin_dir_path( __FILE__ ) . "upload/uploaded_file.csv<br />";
} else {
echo "Temp file was not moved.<br />";
echo '<pre>';
echo 'Here is some more debugging info:<br />';
print_r($_FILES);
print "</pre>";
}
}
}
echo '<form method="post" action="" enctype="multipart/form-data">';
echo '<input type="file" name="file" id="file" />';
submit_button('Import CSV');
echo '</form>';
In the same situation i used: $name = basename($_FILES["file"]["name"]); before calling move_uploaded_file(), i'm not saying it's the solution but maybe you are repeating the path.
Hope it helps.

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'; ?>

ZipArchive::close returns false

Hi I am attempting to create a new zip file, I have recompiled PHP enabling ZIP and here is my code
if($_SERVER['REQUEST_METHOD'] == "POST"){
$zip = new ZipArchive;
if($zip->open("/home/user1joe/public_html/upload/test.zip",ZIPARCHIVE::CREATE)!= true) {
echo "error file did not up upload";
exit(0);
}
foreach ($_FILES["images"]["error"] as $key => $error)
if ($error == UPLOAD_ERR_OK) {
$temp_name = $_FILES["images"]["tmp_name"][$key];
$new_name = $_FILES['images']['name'][$key];
var_dump($temp_name);
var_dump($new_name);
if(file_exists($temp_name)){
$zip->addFile($temp_name,$new_name);
move_uploaded_file($temp_name, "upload/".$new_name);
} else
echo "error file does not exist";
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
}
$res = $zip->close();
var_dump($res);
}
$zip->close() is returning false and no zip is being created.
I get no errors building up to this
$zip->numFiles shows there are files in the archive
The folder where I want to create the zip has writable permission
I am a bit lost of what else I can test for, any ideas would be great!
zip->close() method do some processing (actual compression, or just getting/writing file attributes), which require that files, added to archive, to exist.
However you are removing input files from the original location with move_uploaded_file() call.

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.

Echo "success" after using ftp_put()

I have a php script that sends large files via FTP. After the file is sent I'm trying to write to the browser "success". I'm also trying to send a query to the database to record that the file was sent. However, any code that I have that comes after the ftp_put does not get executed.
if (ftp_put($conn_id, $upload_filename, $filename, FTP_BINARY))
{
echo "File Sent";
echo $upload_filename." - ".date("d/m/Y H:i:s")." - ".filesize($filename)." bytes<br>" ;
}
else
{
echo "Problem while Uploading $filename\n <br/>". $upload_filename ;
}
If ftp_put is false the echo works. But, if the ftp_put is a success any code I put there will not run.
The file size I am sending is 7,305kb
It is likely that the problem here is that your script is timing out while the file is uploading. Try adding this line before the code above:
set_time_limit(0);
The thing is that ftp_put() blocks any further action until the upload is finished. Try ftp_nb_put() (no blocking) like so:
$upload = ftp_nb_put($conn_id, $upload_filename, $filename, FTP_BINARY);
if($upload == FTP_MOREDATA)
{
echo 'Uploading ' . $upload_filename . ' - ' . date("d/m/Y H:i:s") . ' - ' . filesize($filename) . ' bytes<br />';
while($upload == FTP_MOREDATA)
{
echo '.'; //Output a . to page or do whatever
$upload = ftp_nb_continue($conn_id);
}
}
//Note: While in the while above, it will either end in FTP_FINISHED or FTP_FAILED
if($upload == FTP_FAILED)
{
echo "Problem while Uploading $filename\n <br />". $upload_filename;
}

Categories