Upload script returning blank data - php

I have the Uploadify jQuery plugin set up on my site to manage file uploads. Within the onUploadSuccess event, I have this code:
'onUploadSuccess' : function(file, data, response) {
console.log("Upload complete for file " + file.name + ". Script returned: " + data);
}
This is meant to show me whatever the upload script spits out. Now, usually the response is something like this:
Upload complete for file test.jpg. Script returned:
{"status":1,"file":{"id":"v8rwlxj3","name":"test.jpg"}}
The upload script is first accepting the file, then uploading it to Rapidshare using cURL like so:
// Move uploaded file
move_uploaded_file($_FILES['Filedata']['tmp_name'], $targetDir . '/' . $id);
// Get the RapidShare server to upload to
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(!$uploadServer = trim(curl_exec($ch))) {
error('nextuploadserver failed');
}
if(strstr($uploadServer, 'ERROR:')) {
error('nextuploadserver failed');
}
// Upload the file to RapidShare
$uploadID = mt_rand(1000000000, 9999999999);
$url = 'http://rs' . $uploadServer . '.rapidshare.com/cgi-bin/rsapi.cgi?uploadid=' . $uploadID;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
$postFields = array('sub' => 'upload',
'login' => 'login',
'password' => 'password',
'uploadid' => $uploadID,
'filename' => $_FILES['Filedata']['name'],
'filecontent' => '#' . $targetDir . '/' . $id);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
if(!$resp = curl_exec($ch)) {
error('upload call failed');
}
when it's uploaded, the upload script spits out a JSON response like so:
// Output
echo json_encode(array('status' => 1, 'file' => array('id' => $id, 'name' => $uploadDetails[1])));
This works fine for smaller files. When I upload my 30MB test file however, I get this response:
Upload complete for file 30mb.txt. Script returned:
At first I thought PHP was hitting the max execution time, but I have this at the top of my script:
set_time_limit(21600); // 6 hours
And besides, I'd see the PHP error being returned. But it's just not returning anything. What could cause this? Thanks.

Are you sure your upload_max_filesize is set to above 30M? That one has caused me some headaches in the past.

PHP may be not hitting time limit, but ajax may be reaching connection timeout while waiting for the response.

Remove uploadid post parameter. Uploadid parameter with resume upload.

Yes nice answer, although it was in comment but helped me.
http://www.uploadify.com/documentation/uploadify/successtimeout/
set successTimeout to some high value. lets say 1 hour.

Related

php telegram sendPhoto not working (url & file location)

I need some help if possible with php sendPhoto api, I've been using the sendPhoto method in php on my apache server to auto send images into telegram, I've been using this same method for almost 6-7 months and from few days ago suddenly the api method stopped working. I tried passing photo= using the absolute path of file in url and in php using the files directory+filename but sends me an error msg from the api as shown below, first part is my php method which doesnt return any errors, just shows blank
# my php telegram code
$dir = "Attachments/2022/04/09/imagename.jpeg";
$chat_id = '(groupchatid)';
$bot_url = "https://api.telegram.org/bot(mybotapi)/";
$url = $bot_url . "sendPhoto?chat_id=" . $chat_id ;
$post_fields = array('chat_id' => $chat_id,
'photo' => new CURLFile(realpath($dir)),
'caption' =>'Test Image', );
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type:multipart/form-data" ));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);
When i execute this script as it used to work before recently this is the response i get from the API
{
"ok": false,
"error_code": 400,
"description": "Bad Request: invalid file HTTP URL specified: Unsupported URL protocol"
}
If I replace the image URL to another server it send the image successfully, but im unable to send anything only from my server, If I try access the file directly using the URL of my servers image file I can access it from any pc no issue, only problem is telegram fetching the image, please help, appreciate it
Excuse, I don't usually use curl, so I can give you another option:
function sendPhoto($id, $photo, $text = null){
GLOBAL $token;
$url = 'https://api.telegram.org/bot'.$token."/sendPhoto?
chat_id=$id&photo=$photo&parse_mode=HTML&caption=".urlencode($text);
file_get_contents($url);
}
Just declare the sendPhoto function in this way, put the variabile in which you stored the token instead of "$token" and use the parameters in this way:
$id = the id of the user (the one you declared like this: $id = $update['message']['from']['id'];)
$photo = absolute path of the image you want to send
$text = OPTIONAL caption for the image

I need to send an image using "input type="file"" without any restriction on the image size

I'm programming a telegram bot.
I want to send an image to a series of IDs that are stored in my DB (I'M NOT UPLOADING A PHOTO I'M JUST SENDING IT).
The function to send the image works just fine.
The only problem I have is that images that are above 1MB size won't be sended.
I don't upload these images anywhere, I just send them specifying the image url (so it isn't a problem about a max size upload).
/*this is the function that I use to send the image*/
<?php
include "./db.php";
include "../Gestionale-Bar/webhook.php";
$queryID="SELECT DISTINCT acquirente FROM BackupChat ORDER BY acquirente";
$resultID=$conn->query($queryID);
$file =new CURLFile(realpath($_FILES["photo"]["tmp_name"]));
while($rowID = $resultID->fetch_assoc())
{
$url = $website . "/sendPhoto?chat_id=" . $rowID['acquirente'] ;
$post_fields = array('chat_id' => $rowID['acquirente'], 'photo' => $file);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);
}
echo "<script language=\"Javascript\">
window.location.href='mywebpageblablabla';
</script>
";
?>
/*this is the input button where I select the photo*/
function img()
{
var gridWrapper = document.querySelector('.content');
gridWrapper.innerHTML =
"<form action=\"inviaimg.php\" enctype=\"multipart/form-data\" method=\"post\" class=\"inputfile\">" +
"<input type=\"file\" name=\"photo\"/>" +
"<input type=\"submit\" value=\"send\" style=\"background-color:#2a2b30; color:#5c5edc; font-family:AvenirNext; width:10%; height:30px\"></form>"
}
Whenever I try to send an image that is under 1MB everything works fine.
So basically I expect to send photos with bigger size. :)
in your $post_fields you have key photo which value is CURLFile object. In documentation of telegram bots it is written that it belongs pass a value which is file_id as String to send a photo that exists on the Telegram servers, HTTP URL as a String for Telegram to get a photo from the Internet or upload a local photo by passing a file path.
You wrote you didn't upload a file but just sending. Despite this you use a $_FILES[] to get a realpath() of file which is uploaded. Maybe this is a fault of upload_max_filesize. Checkout this.
Check also this this piece of code:
$file = realpath($_FILES["photo"]["tmp_name"]);
while($rowID = $resultID->fetch_assoc())
{
$url = $website . "/sendPhoto?chat_id=" . $rowID['acquirente'] ;
$post_fields = array('chat_id' => $rowID['acquirente'], 'photo' => $file
);
Replace old one with this and give feedback. Greetings, plum!
Sources:
$_FILES[] - https://www.php.net/manual/en/reserved.variables.files.php
The CURLFile class - https://www.php.net/curlfile
Telegram documentation - https://core.telegram.org/bots/api#sendphoto

Curl caching a downloaded image?

I have a script that tails a log file for a song change event. When that happens a Tweet is auto generated and sent to Twitter. Problem is that, I recently updated my script so that it also includes the album art with the Tweet. I am downloading the image using curl, posting the media, then removing the file for the next Tweet. What is throwing me off is that when the image displayed in the Tweet is for the previous song playing and not the current one. I want to know if I download a file using curl, then use php unlink command, would that same image be downloaded again? Here is my code below.
// Analyze prepared tweet for issues with length
$tweeted = ($front . $fixedArtistNow . $hyphen . " " . $fixedTitleNow . $tag);
if (strlen($tweeted) <= 140) {
try {
// $path = ("/home/soundcheck/public_html/images/artwork.png");
// if(unlink($path));
// Grab album art from the song that is currently playing
$fresh = date("y-m-d-G-i-s");
$ch = curl_init('http://soundcheck.xyz:8000/playingart? sid=1?'.$fresh);
var_dump($ch);
$fp = fopen('/home/soundcheck/public_html/images/artwork.png', 'wb');
curl_setopt($fp, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($fp, CURLOPT_FORBID_REUSE, TRUE);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
// copy("/home/soundcheck/public_html/images/artwork.png","/home/soundcheck/public_html/images/album.png");
// $twitter->send($front . $fixedArtistNow . $hyphen . " " . $fixedTitleNow . $tag);
$media1 = $twitter->upload('media/upload', ['media' => '/home/soundcheck/public_html/images/artwork.png']);
// $media2 = $connection->upload('media/upload', ['media' => '/path/to/file/kitten2.jpg']);
$parameters = [
'status' => $tweeted,
'media_ids' => implode(',', [$media1->media_id_string]),
];
$result = $twitter->post('statuses/update', $parameters);
// unlink("/home/soundcheck/public_html/images/artwork.png");
The url I am getting the image from never changes but the image does if that makes any difference. If anyone knows a better way to download the album art, please let me know. Thanks!

File Upload to Slim Framework using curl in php [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Failing to upload file with curl in heroku php app
After handling a file upload in php, I am trying to send that file using curl to my rest api which uses Slim Framework. However, $_FILES is always empty once it reaches the function in Slim.
sender.php
if(move_uploaded_file($_FILES['myFile']["tmp_name"], $UploadDirectory . $_FILES['myFile']["name"] ))
{
$ch = curl_init();
$data = array('name' => 'test', 'file' => $UploadDirectory . $_FILES['myFile']["name"]);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/slimlocation/upload/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
}
And the function to receive the request in Slim:
$app->post('/upload/', function() use ($app) {
if(isset($_FILES)){
// count is always zero
echo count($_FILES);
}
});
Am I sending the file incorrectly and / or is it possible to do what I am attempting? Any help is appreciated.
As far as i know, you need to use more options for a file upload with curl. See here.
Look at the CURLOPT_UPLOAD option and the description of CURLOPT_POSTFIELDS, it says that you need to use an # before the file name to upload (and use a full path).
These were the changes I needed and it worked:
$filepath = str_replace('\\', '/', realpath(dirname(__FILE__))) . "/";
$target_path = $filepath . $UploadDirectory . $FileName;
$data = array('name' => 'test', 'file' => '#'.$target_path);

Multiple file uploads with cURL

I'm using cURL to transfer image files from one server to another using PHP. This is my cURL code:
// Transfer the original image and thumbnail to our storage server
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://' . $server_data['hostname'] . '.localhost/transfer.php');
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
'upload[]' => '#' . $tmp_uploads . $filename,
'upload[]' => '#' . $tmp_uploads . $thumbname,
'salt' => 'q8;EmT(Vx*Aa`fkHX:up^WD^^b#<Lm:Q'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$resp = curl_exec($ch);
This is the code in transfer.php on the server I'm uploading to:
if($_FILES && $_POST['salt'] == 'q8;EmT(Vx*Aa`fkHX:up^WD^^b#<Lm:Q')
{
// Save the files
foreach($_FILES['upload']['error'] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
move_uploaded_file($_FILES['upload']['tmp_name'][$key], $_FILES['upload']['name'][$key]);
}
}
}
All seems to work, apart from one small logic error. Only one file is getting saved on the server I'm transferring to. This is probably because I'm calling both images upload[] in my post fields array, but I don't know how else to do it. I'm trying to mimic doing this:
<input type="file" name="upload[]" />
<input type="file" name="upload[]" />
Anyone know how I can get this to work? Thanks!
here is your error in the curl call...
var_dump($post)
you are clobbering the array entries of your $post array since the key strings are identical...
make this change
$post = array(
'upload[0]' => '#' . $tmp_uploads . $filename,
'upload[1]' => '#' . $tmp_uploads . $thumbname,
'salt' => 'q8;EmT(Vx*Aa`fkHX:up^WD^^b#<Lm:Q'
);
The code itself looks ok, but I don't know about your move() target directory. You're using the raw filename as provided by the client (which is your curl script). You're using the original uploaded filename (as specified in your curl script) as the target of the move, with no overwrite checking and no path data. If the two uploaded files have the same filename, you'll overwrite the first processed image with whichever one got processed second by PHP.
Try putting some debugging around the move() command:
if (!move_uploaded_file($_FILES['upload']['tmp_name'][$key], $_FILES['upload']['name'][$key])) {
echo "Unable to move $key/";
echo $_FILES['upload']['tmp_name'][$key];
echo ' to ';
echo $_FILES['upload']['name'][$key];
}
(I split the echo onto multiple lines for legibility).

Categories