https://trevim.pt/header-4/
I'm copying this image to this link
https://trevim.pt/anuncios/header.png
When I run the script it always says 'File copied successfully' but some times I only get the top half of the image... it's not at all a large file (22.6KB), is this behavior normal? How can I copy the whole image every time? Any ideas? Here's my code:
foreach($to_activate as $data_row)
{
$template_id = $data_row['template_id'];
$img_url = $data_row['img_url'];
$template = mysqli_query($conn, "SELECT name FROM `wp_ad_templates` WHERE id = '$template_id' limit 1")->fetch_object()->name;
$dst = "../" . $template . ".png";
if(!#copy($img_url, $dst))
{
$errors= error_get_last();
echo "COPY ERROR: ".$errors['type'];
echo "<br>".$errors['message']."<br><br>";
} else {
echo "File copied from remote!";
}
}
By the way I found this similar question Php copy only copies part of file which is solved by using exec() but that solution didn't work for me, I get:
COPY ERROR: 2
exec() has been disabled for security reasons
php is getting image in following format, while echoing $_POST['img']
http://localhost/uploads/images/1533033949-8.jpg
But why unlink doesn't working -
// Get src.
$img = $_POST["img"];
// Check if file exists.
if (file_exists(getcwd() . $img)) {
// Delete file.
unlink(getcwd() . $img);
echo "Deleted";
}
I tried testing directly, but doesn't work
unlink($img)
unlink works on the file system, not with HTTP URLs. And appending
#CBroe is correct
First get the base path on you live server
or manually specify the base path like below example
$base_directory = '/home/myuser/';
then unlink the file that you need to remove.
if(unlink($base_directory))
echo "File has been Deleted.";
I hope it helps.
Finally I solved storing url information as variable and php substr, strlen function.
$img=$_POST['img'];
$len = strlen("http://localhost/uploads/");
$new_path = substr($img, $len, strlen($img)-$len);
if(unlink($new_path)){
echo "Deleted";
}
else{
echo "Fail";
}
Hi I have successfully copied file from remote url to mine using copy
if(!#copy('http://www.example.com/download/somedoc.pdf','./pdf/somedoc.pdf'))
{
$errors= error_get_last();
echo "COPY ERROR: ".$errors['type'];
echo "<br />\n".$errors['message'];
} else {
echo "File copied from remote!";
}
But I need to do it manually and one at a time. I did Google for * file name with same extension like somedoc.pdf. anotherdoc.pdf, next.pdf.
Is'nt there anyway to copy automatically to my local server from remote url that is newly uploaded
Found My Ans:
yeah got it.. used simplehtmldom.. The html code or www.example.com was
<a target="_blank" style="text-decoration:none" href="./images/download/somedoc.pdf">somedoc and somename</a>
and my code was
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://www.example.com');
// find all link
foreach($html->find('a[target=_blank]') as $e)
echo $e->href . '<br>';
http://simplehtmldom.sourceforge.net/
now i will convert the output to variable and use the copy function
Thanks for the reponses guys..
JSFIDDLE
I'm using filedrop.js to create a file repository structure within my app. The above noted JSFIDDLE has all of the Javascript / jQuery / HTML and CSS code for this small module. While everything on the client end seems to be functioning properly (files can be DnD'd, progress bar acts correctly, console shows proper event triggers), the result on the server-side is always an empty $_FILES variable. My PHP (ajax.receiveFile.php) is as follows:
var_dump($_FILES);
ob_start();
$callback = &$_REQUEST['fd-callback'];
$job_id = &$_REQUEST['job_id'];
$subdir = &$_REQUEST['subdir'];
$j = loadJob($job_id);
$save_path = "D:\\JobFiles\\" . $j->gOrderNumber() . "\\" . $subdir . "\\";
if ( ($_FILES['fd-file']['size'] > 0) && is_uploaded_file($_FILES['fd-file']['tmp_name']) ) {
$name = $_FILES['fd-file']['name'];
if (move_uploaded_file($_FILES['fd-file']['tmp_name'], $save_path.$name)) {
$j->addAttachment($subdir,$name);
echo 'true';
} else {
echo 'false';
}
}
ob_end_flush();
FileDrop.js seems to be doing what it is supposed to do, as shown here:
I read here on SO that using the same element name over multiple input types of "file" can cause errors but I'm not sure that is the case here. I have double- and triple-checked the permissions on both the TEMP and TARGET upload folders, I have confirmed that all PHP variables are set as needed via visual inspection and PHPINFO(). The server config is PHP 5.4 on IIS7.
If anyone has any ideas on what else to look for, please contribute. Thanks!
This works for me:
file_put_contents('uploads/person/7.jpeg', fopen('php://input', 'r'));
I am trying to convert words to speech ..
Untill now I have tried this:
<?php
$text = "Hello this is a test for voice api of google";
// Name of the MP3 file generated using the MD5 hash
$file = md5($text);
// Save the MP3 file in this folder with the .mp3 extension
$file = "audio/" . $file .".mp3";
if($file) {
echo "created";
} else {
echo "not created";
}
// If the MP3 file exists, do not create a new request
if (!file_exists($file)) {
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?q=' . $text);
echo "hello";
file_put_contents($file, $mp3);
} else {
echo "hii";
}
?>
In my html file :
<audio controls="controls" autoplay="autoplay">
<source src="<?php echo $file; ?>" type="audio/mp3" />
</audio>
I am getting created hello and an audio player in output. But no file is played and neither it is created in the folder?
There is a problem with the url you try to access. It is broken ! You should have tried first.
The new URL, that I found on the FF console is :
http://translate.google.com/translate_tts?ie=UTF-8&q=Hello&tl=en&total=1&idx=0&textlen=5&prev=input
For the single word Hello. And you see that you have to specify the language, and the length of your text in textlen, even though it did work for all the sentences I tried without changing this var.
Another problem is that you have to urlencode() your text, or you will have a bug with accents and punctuation.
So the line to download the MP3 becomes :
// Language of the sentence
$lang = "fr";
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
So the complete code looks like :
<?php
$text = "Bonjour, comment allez vous ?";
// Yes French is a beautiful language.
$lang = "fr";
// MP3 filename generated using MD5 hash
// Added things to prevent bug if you want same sentence in two different languages
$file = md5($lang."?".urlencode($text));
// Save MP3 file in folder with .mp3 extension
$file = "audio/" . $file . ".mp3";
// Check folder exists, if not create it, else verify CHMOD
if (!is_dir("audio/"))
mkdir("audio/");
else
if (substr(sprintf('%o', fileperms('audio/')), -4) != "0777")
chmod("audio/", 0777);
// If MP3 file exists do not create new request
if (!file_exists($file))
{
// Download content
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
file_put_contents($file, $mp3);
}
?>
I found it:
https://translate.google.com.vn/translate_tts?ie=UTF-8&client=tw-ob&q=ANYTHING_TEXT&tl=YOUR_LANGUAGE_CODE
Important: client=tw-ob
YOUR_LANGUAGE_CODE can be en,us,uk,vi etc.
An improved version:
// ~~~ Credits to kube ~~~
$text = "Hello this is a test for voice api of google";
$text = urlencode($text);
$lang = urldecode("en");
$file = "audio/" . md5($text) .".mp3";
if (!file_exists($file) || filesize($file) == 0) {
$mp3 = file_get_contents('http://translate.google.com/translate_tts?ie=UTF-8&q='.$text.'&tl='.$lang.'&total=2&idx=0&textlen='.strlen($text).'&prev=input');
if(file_put_contents($file, $mp3)){
echo "Saved<br>";
}else{
echo "Wasn't able to save it !<br>";
}
} else {
echo "Already exist<br>";
}
You cannot use this service for free.
Is there any free quota?
No, the Google Translate API is only available as a paid service. Please see Pricing and Support for more details. However we do offer the Google Website Translator gadget, which will translate your website without charge.
Check translate API FAQ
More info about this unofficial way of use you can find on Techcrunch
You can also use the simple code below. Just echo the code to get the result. In this code, there is no need to save a file or getting permission problems.
echo "<iframe hidden src='http://translate.google.com/translate_tts?ie=UTF-8&q=Welcome%20back%20".$jvm['firstname']."&tl=en&total=2&idx=0&textlen=5&prev=input'></iframe>";
Your file is not creating because you forgot to create it , use below code for creating the file.
$file = "audio/".$file.".mp3";
$ourFileHandle = fopen($file, 'w') or die("can't open file");