Im trying to pass post file information to an upload.php file and have that information be sent to a CGI script. There is nothing on the net on how to go about doing this that i can find, iv spent days. I know there are a few people out there that need this, it could help us all that have legacy perl scripts.
Dataflow:
Jquery --> Upload.php --> index.cgi
My php:
<?php
if(isset($_FILES['file'])) {
if(move_uploaded_file($_FILES['file']['tmp_name'], "../index.cgi" . $_FILES['file']['name'])){
echo "success";
exit;
}
}
?>
Post call to CGI example:
foobar.com/index.cgi?act=store&data=$filename
Any suggestions would help greatly. Thank you.
From my understanding, your CGI script is receiving a parameter which is the path of the uploaded script. However you are attempting to pass the uploaded script to your CGI script using a function that is only supposed to move a file from 1 place to another without executing a script.
my suggestion is to do the following
<?php
if(isset($_FILES['file'])) {
$destination = "new/path/to/".$_FILES['file']['name'];
if(move_uploaded_file($_FILES['file']['tmp_name'], $destination)){
$data = array();
//You can add multiple post parameters here
//$data = array('param1' => 'value1', 'param2' => 'value2');
$url = "http://url/to/hello.cgi";
// You can POST a file by prefixing with an # (for <input type="file"> fields)
$data['file'] = '#'.$destination;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($handle);
if($result) {
echo "success";
}
exit;
}
}
?>
You can execute the cgi script via a CURL POST and pass any params you want
Related
When I use to following PHP code;
<?php if (file_exists("/foto/Maurice.jpg"))
{
echo "<center><img src='/foto/Maurice.jpg'/></center>";
}
else {
echo "<center><img src='/afbeeldingen/kaars1.png'/></center>";
?>
My browser always shows kaars1.png
instead of Maurice.jpg
I also tried !file_exists but then it doesn't show kaars1.png, when Maurice.jpg doesn't exist.
Is there a simpel way to fix this?
file_exists is only for files on your server's (local) filesystem. You need to actually try to request the URL and see if it exists or not.
You can use cURL to do this.
$handle = curl_init('https://picathartes.com/foto/Maurice.jpg');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_NOBODY, TRUE);
$response = curl_exec($handle);
// Check for 404 (file not found).
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
echo "<center><img src='https://picathartes.com/afbeeldingen/kaars1.png'/></center>";
}
else{
echo "<center><img src='https://picathartes.com/foto/Maurice.jpg'/></center>";
}
curl_close($handle);
(Code from this question: https://stackoverflow.com/a/408416)
This is an answer to your second question
The correct solution depends upon your actual directory structure and the location of the script file in relation to the actual folder and file you are looking for, but to start finding a solution the / in "/foto/Maurice.jpg" say go back to the root directory and look for a directory called /foto
So if this folder is under your DocumentRoot try using
if (file_exists("foto/Maurice.jpg"))
i have a .php file from a videohoster who says that this code is a comandline code which allows me to upload a video to their site and i need to create an interface for a random user that he can upload a video to the videohoster while staying on my website. i thought i simple form like below in html is enought but apparently it doesent work:
front-end, html:
<h2>This form allows you to upload a Video.</h2>
<form action="uploadapi.php" method="post" enctype="multipart/form-data"><br>
<p>Video Name: <input type="text" name="titel" size="50" /></p>
<p>Video Description:<br/><textarea name="text" rows="5" cols="50"> </textarea></p>
<p>Select File, allowed: .mpg </br><input type="file" name="file"></p>
<p><input type="submit" value="Upload"</p>
</form>
the uploadapi.php is supplied by the hoster and so i assume it is correct.
<?php
////////////////////////////////////////////////////////
// for php 5.6+ you need to make some changes in code
// method 1
// add the following line
// curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 0);
//
// method 2
// change
// $post_fields['vfile'] = "#".$file;
// to
// $post_fields['vfile'] = CURLFile($file);
////////////////////////////////////////////////////////
$apiversion = "2.123.20150426";
//REQUIRED Registered Users - You can find your user token in API page.
$user_token = "xxx";
if(count($argv) < 2)
die("Usage: php $argv[0] [VIDEO TO UPLOAD] {SUB FILE}\n");
$file = $argv[1];
if(!file_exists("$file"))
die("ERROR: Can't find '$file'!\n");
$path_parts = pathinfo($file);
$ext = $path_parts['extension'];
$allowed = array("mov");
if (!in_array(strtolower($ext),$allowed))
die("ERROR: Video format not permitted. Formats allowed: .mov!\n");
if(isset($argv[2]))
{
$sub_file = $argv[2];
if(!file_exists("$sub_file"))
die("ERROR: Can't find '$file'!\n");
$path_parts = pathinfo($sub_file);
$ext = $path_parts['extension'];
$allowed = array("srt");
if (!in_array(strtolower($ext),$allowed))
die("ERROR: Subtitle format not permitted. Formats allowed: .srt!\n");
$post_fields['subfile'] = "#".$sub_file;
}
$converter = file_get_contents("http://.../getconv_uploadapi.php? upload_hash=".$user_token);
if($converter=="ERROR")
die("ERROR: Could not choose converter. Aborting... \n");
$post_fields['vfile'] = CURLFile($file);
$post_fields['upload'] = "1";
$post_fields ['token'] = 'xxx';
if(!empty($user_token))
$post_fields['upload_hash'] = $user_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$converter);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($ch);
curl_close ($ch);
echo "$result\n";
?>
is my methodology correct (using html form to call the uploadapi.php on my server) or do i need in order to sumit my video to the videohoster via uploadapi.php other programming languages (ajax, javascript etc)?
Provided file is command-line file. It means it won't work on web as expected.
For example, on web you won't have $argv[1] or $argv[2].
You have two options:
you can rewrite uploadapi.php considering that source file is a file that came from your form
or upload your file and call uploadapi.php as a command-line script with arguments using shell_exec or exec, for example.
i thought that i define or fill the variable $argv[1] from the html form since: type="file" name="file"> = $file = $argv[1];?
... ok rewriting upload api is no alternative since it comes from the hoster.
so what i need to do is uploading the uploadapi.php on my server and then keeping my html form like it is? and insted of action=uploadapi.php i need a action=somethingsomething.php which has an exec command to execute uploadapi.php?:
<?php
function somethingsomething($uploadapi) {
exec($uploadapi . " > /dev/null &");
}
?>
I have a piece of a PHP code:
<?php
$image_cdn = "http://ddragon.leagueoflegends.com/cdn/4.2.6/img/champion/";
$championsJson = "http://ddragon.leagueoflegends.com/cdn/4.2.6/data/en_GB/champion.json";
$ch = curl_init();`
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $championsJson);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
$json_array = json_decode($json, true);
$champions = $json_array["data"];
foreach ($champions as $championdata) {
$image_url = $image_cdn.$championdata["image"]["full"];
$image = file_get_contents($image_url);
file_put_contents("imgfolder/".$championdata["image"]["full"], $image);
}
?>
So the idea of the code is to basically to decode a JSON and to download the images from the following website:
http://gameinfo.na.leagueoflegends.com/en/game-info/champions/
The pictures download perfectly fine and are stored into a folder I have created on my hard drive. The next step is, is it possible for me to use this same piece of code, to download/display the images onto a website I have recently created:
www.lolguides4you.co.uk
the website is basically a day old and I have literally been messing around with some HTML coding. I am new to both HTML and PHP so if someone could point me into the right direction, that would be great!
Assuming that all you want to do it insert the images into a page on your website, then this is quite simple.
However, it may be illegal for you to use an automated scraping tool to save/replicate/duplicate any of the images. Look into that before doing anything on the internet.
The first step is to upload your previous PHP script to your website. That way, rather than downloading the images to your computer and then trying to upload them to your site it allows you to just save the files straight into a directory on the website.
Next, you can create a basic PHP page anywhere on your site:
<?php
echo "Images downloaded:\n";
?>
Next you can use PHP's scandir() function to find every file in the download directory:
<?php
echo "Images downloaded:\n";
$files = scandir('imgfolder/');
foreach($files as $file) {
// Do something
}
?>
Finally, now you just show each image:
<?php
echo "Images downloaded:\n";
$files = scandir('imgfolder/');
foreach($files as $file) {
echo $file . "\n";
}
?>
You could also use Glob, which allows you to ignore any files which aren't a certain type (in case you have other files in the same directory:
<?php
echo "Images downloaded:\n";
$files = glob("imgfolder/*.jpg");
foreach($files as $file){
echo $file . "\n";
}
?>
I wrote this piece of PHP to enable simple PHP File-Uploading with base64-strings.
Back then, it was just supposed to work (and it does). But now I want to beef it up to make this actually secure against malicious intents.
I use this script for an app of mine (file uploads):
if(isset($_GET["upload"]))
{
$contents = $_POST["contents"];
$file = fopen("filename.wav", "w");
$input = base64_decode($contents);
fwrite($file, $input);
fclose($file);
}
You can check if it is an uploaded file via HTTP using is_uploaded_file: http://www.w3schools.com/php/func_filesystem_is_uploaded_file.asp
<?php
$file = "test.txt";
if(is_uploaded_file($file))
{
echo ("$file is uploaded via HTTP POST");
}
else
{
echo ("$file is not uploaded via HTTP POST");
}
?>
I have a website on http://www.reelfilmlocations.co.uk
The above site has an admin area where images are uploaded and different size copies created in subfolders of an uploads/images directory.
I am creating a site for mobile devices, which will operate on a sub-domain, but use the database and images from the main domain,
http://2012.reelfilmlocations.co.uk
I want to be able to access the images that are on the parent domain, which i can do by linking to the image with the full domain i.e http:www.reelfilmlocations.co.uk/images/minidisplay/myimage.jpg
Though i need to check if the image exists first...
I have a php function that checks if the images exists, if it does it returns the full url of the image.
If it doesn't exist i want to return the path of a placeholder image.
The following function i have, returns the correct image if it exists, but if it doesn't, it is just returning the path to the directory where the placeholder image resides i.e http://www.reelfilmlocations.co.uk/images/thumbs/. without the no-image.jpg bit.
The page in question is: http://2012.reelfilmlocations.co.uk/browse-unitbases/
the code i have on my page to get the image is:
<img src="<?php checkImageExists('/uploads/images/thumbs/', $row_rs_locations['image_ubs']);?>">
My php function:
if(!function_exists("checkImageExists")){
function checkImageExists($path, $file){
$imageName = "http://www.reelfilmlocations.co.uk".$path.$file;
$header_response = get_headers($imageName, 1);
if(strpos($header_response[0], "404" ) !== false ){
// NO FILE EXISTS
$imageName = "http://www.reelfilmlocations.co.uk".$path."no-image.jpg";
}else{
// FILE EXISTS!!
$imageName = "http://www.reelfilmlocations.co.uk".$path.$file;
}
echo($imageName);
}
}
Failing to get this to work i did some digging around and read some posts about curl:
This just returns a placeholder image everytime.
if(!function_exists("remoteFileExists")){
function remoteFileExists($url) {
$curl = curl_init($url);
//don't fetch the actual page, you only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);
//do request
$result = curl_exec($curl);
$ret = false;
//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200 ) {
$ret = true;
}
}
curl_close($curl);
return $ret;
}
}
if(!function_exists("checkImageExists")){
function checkImageExists($path, $file){
$imageName = "http://www.reelfilmlocations.co.uk".$path.$file;
$exists = remoteFileExists($imageName);
if ($exists){
// file exists do nothing we already have the correct $imageName
} else {
// file does not exist so set our image to the placeholder
$imageName = "http://www.reelfilmlocations.co.uk".$path."no-image.jpg";
}
echo($imageName);
}
}
I dont know if it could be to do with getting a 403, or how to check if this is the case.
any pointers or things i could try woud be greatly appreciated.
I'd do it using CURL, issuing a HEAD request and checking the response code.
Not tested, but should do the trick:
$URL = 'sub.domain.com/image.jpg';
$res = `curl -s -o /dev/null -IL -w "%{http_code}" http://$URL`;
if ($res == '200')
echo 'Image exists';
The code above will populate $res with status code of the requisition (pay attention that I DON'T include the http:// prefix to the $URL variable because I do it in the command line.
Of course the same might be obtained using PHP's CURL functions, and the above call might not work on your server. I am just explaining what I'd be doing if I had the same need.