当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Function only works 1 time

问题描述:

I wrote a script for uploading an image to my server. Users can use it e.g. for uploading their avatars, or their works.

This is the function in functions.php

function save_image($image, $uploadedfile)

{

error_reporting(0);

$change="";

$abc="";

define ("MAX_SIZE","40000");

function getExtension($str) {

$i = strrpos($str,".");

if (!$i) { return ""; }

$l = strlen($str) - $i;

$ext = substr($str,$i+1,$l);

return $ext;

}

$errors=0;

if ($image)

{

$filename = stripslashes($image);

$extension = getExtension($filename);

$extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))

{

$change='<div class="msgdiv">Unknown Image extension </div> ';

$errors=1;

}

else

{

$size=filesize($uploadedfile);

if ($size > MAX_SIZE*1024)

{

$change='<div class="msgdiv">You have exceeded the size limit!</div> ';

$errors=1;

}

if($extension=="jpg" || $extension=="jpeg" )

{

$uploadedfile = $uploadedfile;

$src = imagecreatefromjpeg($uploadedfile);

}

else if($extension=="png")

{

$uploadedfile = $uploadedfile;

$src = imagecreatefrompng($uploadedfile);

}

else

{

$src = imagecreatefromgif($uploadedfile);

}

echo $scr;

list($width,$height)=getimagesize($uploadedfile);

$newwidth=$width;

$newheight=$height;

$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

$filename = "images/vcard_images/". $image;

imagejpeg($tmp,$filename,100);

imagedestroy($src);

imagedestroy($tmp);

imagedestroy($tmp1);

}}

}

So now I have a form on the page create.php where they can upload 6 of their works. So they select 6 files from their hard drive and then press save. Then I call the function 6 times like this:

$work1 = $_FILES["work1"]["name"];

save_image($_FILES["work1"]["name"], $_FILES['work1']['tmp_name']);

$work2 = $_FILES["work2"]["name"];

save_image($_FILES["work2"]["name"], $_FILES['work2']['tmp_name']);

$work3 = $_FILES["work3"]["name"];

save_image($_FILES["work3"]["name"], $_FILES['work3']['tmp_name']);

$work4 = $_FILES["work4"]["name"];

save_image($_FILES["work4"]["name"], $_FILES['work4']['tmp_name']);

$work5 = $_FILES["work5"]["name"];

save_image($_FILES["work5"]["name"], $_FILES['work5']['tmp_name']);

$work6 = $_FILES["work6"]["name"];

save_image($_FILES["work6"]["name"], $_FILES['work6']['tmp_name']);

But the weird part is, if I press SAVE, the page just becomes white. And if I only call the function onces, so only this:

$work1 = $_FILES["work1"]["name"];

save_image($_FILES["work1"]["name"], $_FILES['work1']['tmp_name']);

It works perfectly fine..

No idea what I'm doing wrong..

Kind regards

EDIT:

Error reporting told me this:

Notice: Constant MAX_SIZE already defined in /Applications/XAMPP/xamppfiles/htdocs/vcard2/user/functions.php on line 11

Fatal error: Cannot redeclare getExtension() (previously declared in /Applications/XAMPP/xamppfiles/htdocs/vcard2/user/functions.php:12) in /Applications/XAMPP/xamppfiles/htdocs/vcard2/user/functions.php on line 12

网友答案:

I would recommend you to have a look at HTML5's FILE API, which manages multiple files uploading and Drag and Drop system. two recommended scripts if you want to play around:

http://blueimp.github.com/jQuery-File-Upload/

and

https://github.com/weixiyen/jquery-filedrop

i tried them both and they are nice

网友答案:

You have a typing error: echo $scr instead of echo $src

网友答案:

Your errors are two seperate but are happening for the same reason:

Notice: Constant MAX_SIZE already defined in /Applications/XAMPP/xamppfiles/htdocs/vcard2/user/functions.php on line 11

You write DEFINE inside your function. The first time it runs it works fine, but the seconds time, it is already defined. Move DEFINE out of the function save_image

Fatal error: Cannot redeclare getExtension() (previously declared in /Applications/XAMPP/xamppfiles/htdocs/vcard2/user/functions.php:12) in /Applications/XAMPP/xamppfiles/htdocs/vcard2/user/functions.php on line 12

The same goes for this error. The seconds time the function runs the function getExtention is redicleared. Move the function outside of save_image and it should be working as expected.

网友答案:

Your function contains a define statement. This is executed every time you call the function, but define fails when you try to re-define an existing constant.

Place this define statement outside of the function, instead.

Same story for that function getExtension: it's not clear to me why you're doing that inside another function; don't.

网友答案:

Move the function getExtension out of the save_image function.

Nested functions in PHP are not bound to the "private" scope of the function, they come into the global function table. And you can't redclare a (global) function.

If you really want to nest functions, then declare when nondeclared only:

if (!function_exists('getExtension')) { function getExtension(){...}}  

You should consider to move the define Statement also out of the function or make it conditional :

if (!defined('MAX_SIZE')) { define('MAX_SIZE', 1024); }

And you could also check the Requestvars with isset or empty, ie. in a loop:

foreach (range(1,7) as $num) { 
  if (!empty($_FILES['work'.$num]['tmp_name'])) { 
    save_image($_FILES["work".$num]["name"], $_FILES['work'.$num]['tmp_name']); 
  } 
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: