0

我有一个表单,其中有一个名称为“foto”的输入类型文件

目标是在进入数据库之前调整用户正在上传的照片的大小。

主要错误是,当我点击提交时,页面显示大量奇怪的代码,如下所示:

����JFIF��m���b~bs�I$��'$V�ڳ�v,HL���rr0{ �r�I4����nCT�����O���%�vw|��;��[쯧��!VOݓI&Ҽ�M춶��o�Z�ѥ��Vb���������� ۧ��b��zi8Pr�%�9 ��猞3!�Dx�,U�8t�F�cМ�X��lP�

如果我点击提交,我会按照以下说明进行操作

$fFoto="";

if($_FILES['foto']['error'] == UPLOAD_ERR_OK) {

     $fFile="/upload/inserzionisti/".$id."_".$_FILES["foto"]["name"];
     $fFoto=$id."_".$_FILES["foto"]["name"];

     $percent = 0.5;

     list($width,$height) = getimagesize("upload/inserzionisti/".$fFoto."");
     $new_width = $width * $percent;
     $new_height = $height * $percent;

     $image_p = imagecreatetruecolor($new_width,$new_height);
     $image = imagecreatefromjpeg("upload/inserzionisti/".$fFoto."");

        imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);
     imagejpeg($image_p,null,100);

     move_uploaded_file($_FILES["foto"]["tmp_name"],".".$fFile);
     $sql = "UPDATE inserzionisti SET FotoUrl ='$fFoto' where Id=$id";

你有什么建议吗?

谢谢

4

1 回答 1

0

试试这个代码:

$fFoto="";

if($_FILES['foto']['error'] == UPLOAD_ERR_OK) {
  $fFile="/upload/inserzionisti/".$id."_".$_FILES["foto"]["name"];
  $fFoto=$id."_".$_FILES["foto"]["name"];

  $percent = 0.5;

  list($width,$height) = getimagesize("upload/inserzionisti/".$fFoto."");
  $new_width = $width * $percent;
  $new_height = $height * $percent;

  $image_p = imagecreatetruecolor($new_width,$new_height);
  // create the new image from the uploaded file
  $image = imagecreatefromjpeg($_FILES["foto"]["tmp_name"]);

  imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);
  // add the new path as second parameter of imagejpeg() to save the file in the specified location
  imagejpeg($image_p,$fFile,100); 

  // the following line isn't necessary anymore
  // move_uploaded_file($_FILES["foto"]["tmp_name"],".".$fFile);
  $sql = "UPDATE inserzionisti SET FotoUrl ='$fFoto' where Id=$id";
于 2013-10-24T10:03:22.777 回答