S
S
Sergey Uspenskiy2017-03-26 20:30:19
PHP
Sergey Uspenskiy, 2017-03-26 20:30:19

Saves broken jpg, news parsing script, what's wrong?

The script parses the news, downloads the image from the link, saves the broken file to the server, what's wrong? :
if($f_Link){
$c=file_get_contents($f_Link);
$fh=fopen($_SERVER['DOCUMENT_ROOT']."/scripts/news.txt","w");
fwrite($fh,$c);
fclose($fh);
preg_match("/b-material-head__title\">(.{1,})<\/h1>/", $c, $h1);
preg_match("/class=\"b-material-head__date-day\ ">(.{10})<\/span>/", $c, $date);
preg_match("/link rel=\"image_src\" href=\"(.*)\">/U", $c, $img); -save image
preg_match("/(\C*<\/article>)/U", $c, $text);
$id=mysql_insert_id($LinkID);
if($img[1 ]){
$ext= strrchr($img[1],
mysql_unbuffered_query("INSERT INTO Message5 (User_ID,Subdivision_ID,Sub_Class_ID,Priority,Checked,Created,parent_class_id,parent_mess_id,foto) VALUES(1,'19','19',20,1,NOW(),'2',' ".$message."','str".$ext.":image/jpeg:1');", $LinkID);
$id=mysql_insert_id($LinkID);
copy($img[1],$_SERVER['DOCUMENT_ROOT']."/sysfiles/45_".$id.$ext);
}
}
-- -- There
d22fdb0c5a274ab3b2cb1a19298e9e1e.jpga0b2cf7262e44243a1085b5f6636c87a.jpg
is
a mini-picture, only it is in the body of the news, but should be at the top in the headline.
The script used to work without problems, the layout on the Russian newspaper website was updated, I am correcting regular expressions and I can’t solve the problem with the image in any way. Maybe someone sees the error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-03-27
@slo_nik

Goodnight.
The problem is in the line:
$img['1'] contains '//cdnimg.rg.ru/img/content/138/31/30/pr_t_650x433.jpg'.
Try doing this:
The file will copy without problems.
And do checks so that you can determine the error yourself.
Do the same as you do now:

if(!copy($img[1],$_SERVER['DOCUMENT_ROOT']."/sysfiles/45_".$id.$ext)){
   echo 'error';
}
else{
 echo 'success';
}

Without specifying the data transfer protocol, there should be a warning, add https and everything should work.
And look at the server logs, there should be this warning.
And the regular expression pattern itself can be tweaked.
ps
And in line
there is no saving/copying the image)
ps
This code
$fh=fopen($_SERVER['DOCUMENT_ROOT']."/scripts/news.txt","w");
fwrite($fh,$c);
fclose($fh);

can be replaced with file_put_contents()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question