Echo "success" after using ftp_put() - php

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;
}

Related

Imagejpeg returns false but file exists

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");

Replace uploaded document on confirm not working

My Requirement is as follows:
When user uploads a file i should check for "File already Exists", if file exists i must show confirm box if 'OK' i have to replace and if cancel the reverse.
This is my following code
if (file_exists($path . $documentName)) {
$msg = $documentName . " already exists. ";
?>
<script type="text/javascript">
var res = confirm('File already exists Do you want to replace?');
if (res == false) {
<?php
$msg = 'File Upload cancelled';
?>
} else {
<?php
if (move_uploaded_file($_FILES["document"]["tmp_name"], $path . $documentName)) {
$msg = $documentName . " File Replaced Successfully";
$successURL = $document_path . $documentName;
}
else
$msg = $documentName . "Upload Failed";
?>
}
</script>";
<?
}
My problem is even if i give cancel the file is getting replaced.
just let me know where I'm wrong or Is there any other approach?
Please help me to close this issue
Note:jquery Not allowed.
Your problem is that you mix javascript and PHP. The PHP-Code will be run on the server and generates the HTML-document. At this point, the file gets replaced already.
Then, this document (with the javascript-code inside) will then be send to the user and there the javascript-code is run. And in that moment, the user gets to see the confirmaion-dialog, even though the file already was replaced!
Take a look at the source-code that your php-code is generating and you will see what I mean.
A solution would be to add a checkbox to confirm overwriting files. Then after hitting the upload-/submit-button, your php-script would check if this box was checked and either replace the file or not.
#Gogul, honestly, this is not the right way to go. Better that you handle the file submission with an AJAX request which receives a response back from your server (either uploaded successfully, or file exists) which you handle appropriately. If presenting the user an option to replace the file, again handle that action with AJAX.
You can do AJAX request in raw JavaScript (jQuery not required) - see here: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
You are mixing server side code with client side javascript. The solving of your problem is more complicated if you don't want the user to reupload the document:
Store the file in a temporary location under random filename. Output a yes/no form to the user, including the random filename and original filename.
If the user answers yes, move from temporary location to $path, else remove the file from temporary location.
Guys i came with with this following solution
upload
uploaddocument.php
$documentName = preg_replace('/[^a-zA-Z0-9.]/s', '_', $_FILES["document"]["name"]);
if (file_exists($path . $documentName)) {
move_uploaded_file($_FILES["document"]["tmp_name"], "F:\\Content\\enews_files\\temp\\" . $documentName);
$msg = $documentName . " already exists. <a href='confirm.php?confirm=1&filename=" . $documentName . "&language=" . $lang . "'>Replace</a>||<a href='confirm.php?confirm=0&filename=" . $documentName . "'>Cancel</a>";
} else {
if (move_uploaded_file($_FILES["document"]["tmp_name"], $path . $documentName)) {
$msg = $documentName . " Upload Success";
$successURL = $document_path . $lang . '/' . $documentName;
}
else
$msg = $documentName . " Upload Failed";
}
confirm.php
include("config_enews.php");
$lang = $_GET['language'];
$path = "F:\\Content\\enews_files\\" . $lang . "\\";
//$path = "D:\\test\\test\\" . $lang . "\\";
$documentName = preg_replace('/[^a-zA-Z0-9.]/s', '_', $_GET["filename"]);
if ($_GET['confirm'] == 1) {
//echo sys_get_temp_dir();die;
if (copy("F:\\Content\\enews_files\\temp\\" . $_GET["filename"], $path . $documentName)) {
unlink("F:\\Content\\enews_files\\temp\\" . $_GET["filename"]);
header("Location: uploaddocument.php?message=success&fname=$documentName&lang=$lang");
} else {
echo $res = move_uploaded_file($_GET["tempname"], $path . $documentName);
echo $msg = $documentName . " Upload Failed";
header("Location: uploaddocument.php?message=failed&fname=$documentName");
}
} else {
unlink("F:\\Content\\enews_files\\temp\\" . $_GET["filename"]);
header("Location: uploaddocument.php?message=cancelled&fname=$documentName");
}
I got this spark from #Marek. If any one has better solution kindly provide.
I don't have enough reputations to vote your answers sorry.
Thank you so much for all your support.

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.

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().

PHP mail inside function with if/else statements

Trying to get my script to send me an email if the file uploads successfully. Here is my part of the script that saves the file to the server as username.site.zip:
if ($this->file->save($uploadDirectory . $_SESSION['myusername'] . '.site.' . $ext)){
return array('success'=>true);}
else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
I'm not quite sure how to add in the mail function so that if it's success=>true then it sends mail('email#domain.com','subject','body');
Any help would be great.
<?php
if ($this->file->save($uploadDirectory . $_SESSION['myusername'] . '.site.' . $ext)){
$message = "The file was successfully uploaded.";
mail('email#domain.com', 'My Subject', $message);
return array('success'=>true);
} else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
?>
You had it. Not sure what you still needed.
Insert your mail command right before the return of the success=>true array like this:
if ($this->file->save($uploadDirectory . $_SESSION['myusername'] . '.site.' . $ext)){
mail('email#domain.com','subject','body');
return array('success'=>true);}
else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
Maybe encapsulate it in a try-catch block, but this should work right out of the box.

Categories