简体   繁体   中英

Having trouble with the php command “imagepng” creating a file with unique name

Hello I am having trouble getting imagepng to not only create a image on my server but name it a specific name according to a POST input from a form on another page.

It will display the finished image on screen just fine but depending on which method I try (tried many), it either creates a file name ".png" or does not create one at all.

Here is the "form.html" the info is pulling from:

<form action="test.php" method="post"><BR><BR>
Type Forum Name:
<input type="text" name="name" value=""/><BR><BR>
<input type="submit" name="Submit1"><BR><BR>
</form><BR> 

And here is "test.php":

<?php
$name = $_POST["name"];

$input1 = $_POST["fsbg"];

$input2 = $_POST["rank"];

header ("Content-type: image/png");
$background = imagecreatefrompng($fsbg);
$pkt = imagecreatefromgif($rank);
imagecopymerge($background,$pkt,260,136,0,0,55,55,100);
imagepng($background);
$save = strtolower($name) .".png";
imagepng($background, $save);
imagedestroy($background);
imagedestroy($pkt);
header('location:link.php');
?>

I have set folder/sub folder permissions to 777, tried saving in the same path as the file, as well as saving in a sub folder called "pvt".

Any help/Advice would be greatly appreciated, Thanks!

You do not have a "name" field in the form. You have a unnamed field whose value is "name":

<input type="text" value="name"/><BR><BR>

Needs to be:

<input type="text" name="name" value="name"/><BR><BR>

Also, when you do this, sanitize the input. What if I sent you name="../../etc/passwd" or something like that? ( of course this example won't work. But still. ).

At the very least use basename to strip all path components from the name, and realpath to ensure you're writing where you should.

I finally figured this out.

Turns out even though I was changing the Permissions in my FTP, I still needed to add this in to get it to save a file: chmod($name,0755);

Completed code looks like:

$background = imagecreatefrompng($fsbg);
$pkt = imagecreatefromgif($rank);
//$pkt2 = imagecreatefromgif($rank);
imagecopymerge($background,$pkt,260,136,0,0,55,55,100);
//imagecopymerge($background,$pkt2,290,136,0,0,55,55,100);
header( "Content-type: image/png" );
imagepng($background);
$name = $filename.".png";
chmod($name,0755);
imagepng($background, $name);
imagedestroy($background);
imagedestroy($pkt);

Thanks to all who tooks the time to reply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM