Need help to make this curl script multi links - php

Here is the script it downloads files from url. The thing that I want is multi links like there should be three or more input url boxes in which user puts their links and the script downloads all the files. I don't want to press a button and another url box appear; that is not I want, I have already tried that. Or multi links; something like this where we can put links on each line:
<?php
class Download {
const URL_MAX_LENGTH=2000;
// clean url
protected function cleanUrl($url){
if (isset($url)){
if (!empty($url)){
if(strlen($url)< self::URL_MAX_LENGTH){
return strip_tags($url);
}
}
}
}
//is url
protected function isUrl($url){
$url=$this->cleanUrl($url);
if (isset($url)){
if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)){
return $url;
}
}
}
//return extension
protected function returnExtension($url){
if ($this->isUrl($url)){
$end = end(preg_split("/[.]+/", $url));
if (isset($end)){
return $end;
}
}
}
// file download
public function downloadFile($url){
if ($this->isUrl($url)){
$extension = $this->returnExtension($url);
if ($extension){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
// directory where files should be downloaded
$destination = "uploads/file.$extension";
$file = fopen($destination, "w+");
fputs($file, $return);
if (fclose($file)) {
echo "Successfully Download The File";
}
}
}
}
}
$obj = new Download();
if (isset($_POST['url'])) { $url = $_POST['url'];}
?>
<form action="index.php" method="post">
<input type="text" name="url" maxlength="2000">
<input type="submit" value="Download" />
</form>
<?php if (isset($url)) { $obj->downloadFile($url); }?>

Break string into array using \n as delimiter, you will get array of URLs. check below example to use explode.
Note: Use <textarea>, if you use input and press enter then form will get submitted.
<form action="" method="post">
<textarea type="text" name="url" maxlength="2000"></textarea>
<input type="submit" value="Download" />
</form>
<?php
if(isset($_POST['url'])){
$urls = explode("\n",$_POST['url']);
}
foreach ($urls as $url) {
echo $url;
//$obj->downloadFile($url);
}
?>

Related

Open php file, change value for one variable, save

I am trying to modify the value of a variable $denumire produs=' '; from a php file _inc.config.php through a script by this code with form from file index.php and i have some errors.
The new value of the variable will become value entered from the keyboard via the form.
Anyone can help me, please?
<?php
if (isset($_POST['modify'])) {
$str_continut_post = $_POST['modify'];
if (strlen($_POST['search']) > 0 || 1==1) {
$fisier = "ask003/inc/_inc.config.php";
$fisier = fopen($fisier,"w") or die("Unable to open file!");
while(! feof($fisier)) {
$contents = file_get_contents($fisier);
$contents = str_replace("$denumire_produs =' ';", "$denumire_produs ='$str_continut_post';", $contents);
file_put_contents($fisier, $contents);
echo $contents;
}
fclose($fisier);
die("tests");
}
}
?>
<form method="POST" action="index.php" >
<label>Modifica denumire baza de date: </label>
<input type="text" name="den">
<button type="submit" name="modify"> <center>Modifica</center></button>
</div></div>
</form>
This is an XY problem (http://xyproblem.info/).
Instead of having some sort of system that starts rewriting its own files, why not have the file with the variable you want to change load a json config file?
{
"name": "Bob",
"job": "Tea Boy"
}
Then in the script:
$json = file_get_contents('/path/to/config.json');
$config = json_decode($json, true);
$name = $config['name'];
Changing the values in the config is as simple as encoding an array and putting the json into the file.
$config['denumireProdu'] = 'something';
$json = json_encode($config);
file_put_contents('/path/to/config.json', $json);
This is far saner than getting PHP to rewrite itself!
Docs for those commands:
http://php.net/manual/en/function.json-decode.php
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/function.file-put-contents.php

Secure Direct File Downloader PHP Needs Fixes

Unfortunately, This code isn't working because i wanna download files through php and need to hide the direct path of files uploaded in my server.
If i define complete path in variable.. example.com/files/filedownload.iso then it's working but it's pointless, because i wanna hide a path while downloading.
<form target="_blank" id="download_file" action="download.php" method="post">
<input name="ip" type="hidden" value="192.123.23.1">
<input name="filename" type="hidden" value="filedownload.iso"'; ?>
<div align="center">
<input alt="Submit" src="download.gif" type="image" />
</div>
</form>
The above code is POST method..
<?php
if(isset($_POST['ip']) && $_POST['ip']!="" && isset($_POST['filename']) && $_POST['filename']!=""){
$filename = $_POST['filename'];
}
$domain="http://example.com/".$filename;
//$redirect_url="http://example.com".$filename;
$redirect_url=$path;
$redirect_url= encrypt_download_link($domain,$path);
?>
<script type="text/javascript">
var max_time= 5; //Seconds
function Redirect()
{
window.location="<?php echo $redirect_url; ?>";
}
function refresh_remaining_time()
{
max_time = max_time-1;
if (max_time>=0) {
document.getElementById("waiting_time_span").innerHTML = max_time+" Seconds";
}
}
window.onload = function() {
setInterval(function () {
if (max_time>=0) {
refresh_remaining_time();
}
}, 1000); // Execute somethingElse() every 2 seconds.
setTimeout(function () {
Redirect();
}, 5000);
};
</script>
<?php
}
function encrypt_download_link($domain,$path){
$secret = '4rTyHHgtopSUm';
$expire = strtotime("+7 days");
$md5 = base64_encode(md5($secret.$path.$expire,true));
$md5 = strtr($md5, '+/', '-_');
$md5 = str_replace('=', '', $md5);
$url = $domain.$path."?st=".$md5."&e=".$expire;
return $url;
}
?>
I'm not sure if this would work for you, and it won't work for large files, but if you redirect users to a page with this code it will stream the file in binary down to their system. Don't be a hater if your files are larger and this won't work :)
P.S. I'd love to take credit for this, and I searched for the source (couldn't find it), but this hit my library at someone else's suggestion a few years back.
$nameFile = 'insert just name of file here'
$pathFile = 'insert file and path here';
$sizeFile = filesize($pathFile);
$pointerFile = fopen($pathFile, "rb"); // Open file for reading in binary mode
$contentFile = fread($pointerFile, $sizeFile);
fclose($pointerFile);
header("Content-length: ".$sizeFile);
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=".$nameFile.";" );
echo $contentFile;

PhP Curl SFTP File Editing

I am trying to create a online editor for multiple server. I want to edit a custom file on a server and I need to get it via sftp. My current code looks like this:
<?php
$user="user";
$pass = 'pass';
$c = curl_init("sftp://$user:$pass#0.0.0.0/path/to/file/file.txt");
curl_setopt($c, CURLOPT_PORT, 3206);
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($c, CURLOPT_FILE, $fh);
curl_exec($c);
curl_close($c);
//the next line is not working and from now on am I stuck
$text = file_get_contents($fh);
?>
<!-- HTML form -->
<form action="" method="post">
<textarea name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input
type="submit" />
<input type="reset" />
</form>
I want to edit this file on the website and then reupload it to the sftp server in the same directory (overweite the existing one). I do not know how to continue. Thanks for the help.
First off if it's a file containing some programming language, check ACE.js.
It's simply an incredible JS module for use as a web IDE and it has all the features any programmer looks for in an IDE, it's so good I would ALMOST consider switching to it as my primary IDE.
Then use this PHP code:
<?php
$_POST = json_decode(file_get_contents('php://input'), true);
$filename = 'sftp://user#location/file/name.js';
//Use SSH public key authentication
$handle = fopen($filename,'w') or die('Cannot open file: '.$filename);
$data = $_POST['src'];
fwrite($handle, $data);
fclose($handle);
?>
and use this JS code to call the PHP script:
<script src="../scripts/ace/ace.js" type="text/javascript"></script>
<script>
var editor = ace.edit("editor");
editor.setTheme('<?php echo $theme; ?>');
editor.getSession().setMode("<?php echo $language; ?>");
editor.setShowPrintMargin(false);
editor.setReadOnly(true);
<?php //Save shortcut binding ?>
editor.commands.addCommand({
name: 'Save',
bindKey: {win: 'Ctrl-S', mac: 'Command-S'},
exec: function(editor) {
var xhr = new XMLHttpRequest();
xhr.open("POST", './scripts/save_file.php', true);
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.send(
JSON.stringify(
{src:editor.getValue()}
)
);
}
});
</script>

PHP - Obtain data from an external script and use it in the current form

I have created a script that will work as a plugin for Wordpress whose purpose is to get the data of the videos of a page (video title, video url, thumb url and total videos found).
My plugin works but the script is on the same page where the results are loaded so that it stays in Wordpress. But I want to externalize the script in charge of doing the search because I sincerely do not want them to be able to visualize my php source code.
So I tried to separate my script and just put the html and invoke the script using the "action" form.
But I do not know how to pass the loops of my external script to the local form nor how to pass the values of the variables. I tried to use the "return" and "Header Location" but it does not work.
Here is what I currently have:
Index.php:
<?php
if (isset($_POST['search'])){
$url = $_POST['keyword'];
$parse = "http://example1.com/?k=".$url;
$counter = 0;
$html = dlPage($parse); //dlPage is a function that uses "simple_html_dom"
include("form_results_footer.php"); // I include the header of the page with the results (this part is outside the loop because I do not want it to be repeated).
foreach ($html->find('something') as $values) {
//Here I run a series of searches on the page and get the following variables.
$counter++;
$title = //something value;
$linkvideo = //something value;
$thumburl = //something value;
include("form_results.php"); //The results of the "foreach" insert them into a separate php in "fieldsets".
}
$totalvideos = $counter;
include("form_results_footer.php"); //Close the form results
} else {
?>
<html>
<form action="" method="post">
<input id="keyword" name="keyword" type="text">
<button id="search" name="search">Search</button>
</form>
</html>
<?php
}
?>
Ok, the code above works fine but I need to outsource the part where I get the variables of the part where I will receive them, something like this:
-> http://example.com/script.php
<?php
if (isset($_POST['search'])){
$url = $_POST['keyword'];
$parse = "http://example.com/?k=".$url;
$counter = 0;
$html = dlPage($parse); //dlPage is a function that uses "simple_html_dom"
foreach ($html->find('something') as $values) {
//Here I run a series of searches on the page and get the following variables.
$counter++;
$title = //something value;
$linkvideo = //something value;
$thumburl = //something value;
}
$totalvideos = $counter;
return $title;
return $linkvideo;
return $thumburl
}
?>
index.php
<html>
<form action="http://example.com/script.php" method="post">
<input id="keyword" name="keyword" type="text">
<button id="search" name="search">Search</button>
</form>
</html>
The loop results would have to be collected on the same page in the same way as in the initial example.
I hope you let me understand, thank you in advance.
Change form action url to self page in index.php and do cURL there which calls your another domain where logic is placed. that mean http://example.com/script.php
index.php
<html>
<form action="" method="post">
<input id="keyword" name="keyword" type="text">
<button id="search" name="search">Search</button>
</form>
</html>
<?php
if (isset($_POST['search'])) {
//your URL
$url = "http://example.com/script.php?keyword=" . $_POST['keyword'];
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Execute
$result = curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
}
?>
And in http://example.com/script.php you just need to change $_POST['keyword'] to $_GET['keyword'] and few more change to return data.
scrip.php (from another domain)
<?php
if (isset($_GET['keyword'])) {
$url = $_GET['keyword'];
$parse = "http://example.com/?k=" . $url;
$counter = 0;
$html = dlPage($parse); //dlPage is a function that uses "simple_html_dom"
$return = array(); //initialize return array
foreach ($html->find('something') as $values) {
//Here I run a series of searches on the page and get the following variables.
$return['video_data'][$counter]['title'] = //something value;
$return['video_data'][$counter]['linkvideo'] = //something value;
$return['video_data'][$counter]['thumburl'] = //something value;
$counter++;
}
$return['total_video'] = $counter;
echo json_encode($return); //return data
}
?>
I hope this is what you want.

Merge 2 php scripts

I need some help with some php scripting. I wrote a script that parses an xml and reports the error lines in a txt file.
Code is something like this.
<?php
function print_array($aArray)
{
echo '<pre>';
print_r($aArray);
echo '</pre>';
}
libxml_use_internal_errors(true);
$doc = new DOMDocument('1.0', 'utf-8');
$xml = file_get_contents('file.xml');
$doc->loadXML($xml);
$errors = libxml_get_errors();
print_array($errors);
$lines = file('file.xml');
$output = fopen('errors.txt', 'w');
$distinctErrors = array();
foreach ($errors as $error)
{
if (!array_key_exists($error->line, $distinctErrors))
{
$distinctErrors[$error->line] = $error->message;
fwrite($output, "Error on line #{$error->line} {$lines[$error->line-1]}\n");
}
}
fclose($output);
?>
The print array is only to see the errors, its only optional.
Now my employer found a piece of code on the net
<?php
// test if the form has been submitted
if(isset($_POST['SubmitCheck'])) {
// The form has been submited
// Check the values!
$directory = $_POST['Path'];
if ( ! is_dir($directory)) {
exit('Invalid diretory path');
}
else
{
echo "The dir is: $directory". '<br />';
chdir($directory);
foreach (glob("*.xml") as $filename) {
echo $filename."<br />";
}
}
}
else {
// The form has not been posted
// Show the form
?>
<form id="Form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Path: <input type="text" name="Path"><br>
<input type="hidden" name="SubmitCheck" value="sent">
<input type="Submit" name="Form1_Submit" value="Path">
</form>
<?php
}
?>
That basically finds all xmls in a given directory and told me to combine the 2 scripts.
That i give the input directory, and the script should run on all xmls in that directory and give reports in txt files.
And i don't know how to do that, i'm a beginner in PHP took me about 2-3 days to write the simplest script. Can someone help me with this problem?
Thanks
Make a function aout of your code and replace all 'file.xml' to a parameter e.g. $filename.
In the second script where the "echo $filename" is located, call your function.

Categories