用php上传照片

时间:2015-02-26 18:09:49

标签: php html mysqli photos

我有一个照片库我试图为这个社交网络创建,虽然我上传部分有问题。我有预上传表格,可以按照我的要求运作。有一个字段可以浏览照片,添加标题并选择要添加照片的图库(类别来自数据库)。表单操作(upload.php)似乎是问题的起源,它会停止。

致命错误:第17行的upload.php超过了20秒的最长执行时间

以下是"上传部分" - 问题源自哪里

<?php
require 'configphot.inc.php';
$photos_uploaded=$_FILES[photo_filename];
$photo_caption=$_POST['photo_captions'];



$photo_type=array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg', 
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png',
);

while($counter <= count($photos_uploaded)){
if($photos_uploaded['size'][$counter] > 0){
if(!array_key_exists($photos_uploaded['type'][$counter], $photo_type))
{
$final_result .= 'File' . ($counter +1) . 'is not a photo<br />';

}
else{
mysqli_query("
INSERT INTO gallery_photos (
photo_filename,
photo_caption,
photo_category
) VALUES (
'0',
'". $photo_captions[$counter] . "',
'". $_POST['category'] . "'
)
");

$new_id=mysqli_insert_id();

$filetype= $photos_uploaded['type'][$counter];
$extension = $known_photo_types[$filetype];
$filename = "$new_id.$extension";

mysqliquery ("
UPDATE gallery_photos SET
photo_filename= '$filename'
WHERE photo_id = '$new_id'


");

copy($photos_uploaded['tmp_name'][$counter], $images_dir . '/' . $filename);

$size = GetImageSize($images_dir . "/" . $filename);    

// Wide Image    
if($size[0] > $size[1])    
{     
 $thumbnail_width = 100;     
 $thumbnail_height = (int)(100 * $size[1] / $size[0]);     
}     

// Tall Image    
else    
{    
$thumbnail_width = (int)(100 * $size[0] / $size[1]);    
$thumbnail_height = 100;    
}

$gd_function_suffix= array (
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG', 
'image/gif' => 'GIF',
'image/bmp' => 'BMP',
'image/x-png' => 'PNG',
);

$function_suffix = $gd_function_suffix[$filetype]; //  get name suffix on basis of type
$function_to_read = 'ImageCreateFrom' . $function_suffix; // build function to read file
$function_to_write = 'Image'. $function_suffix; // function to write

$source_handle=$function_to_read($images_dir . '/' . $filename); //Read source file

if ($source_handle) {
$destination_handle = ImageCreateTrueColor($thumbnail_width,     $thumbnail_height); //creates blank image for thumbnail

ImageCopyResampled($destination_handle, $source_handle, 0,0,0,0,         $thumbnail_width, $thumbnail_height, $size[0], $size[1]); //image resize

$function_ti_write($destination_handle, $images_dir . '/tb_' . $filename);

}





}


}
}





?>

这是预上传,工作正常,但仅适用于上下文

<?php
require 'configphot.inc.php';



$_photo_upload_fields='';
$counter=1;

$nofields=(isset($_GET['number_of_fields']))?
(int) ($_GET['number_of_fields']):1;
$result = mysqli_query($link,"SELECT category_id, category_name FROM photo_category");

while($row = mysqli_fetch_array($result)) {  



$photo_category_list .="<option value =" .$row['category_id'] . ">" .  $row['category_name'] . "</option>";


}  



mysqli_free_result($result);


while($counter<=$nofields)
{
$photo_upload_fields .= <<<__HTML_END

<tr><td>
Photo{$counter}:
<input name = "photo_filename[]" type= "file" />
</td></tr>
<tr><td>
Caption:
<textarea name ="photo_caption[]" cols = "30" rows ="1"></textarea>
</td></tr>
__HTML_END;
$counter++;
}

//End output

echo <<<__HTML_END

<html>
<head>
<title> Upload photos here</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post" name= "upload_form">
<table width="90%" border="0" align="center" style="width: 90%;">
<tr>
<td>Select Category
<select name="category">
$photo_category_list</select></td>
</tr>

<!image fields here -->

$photo_upload_fields
<tr><td>
<input type ="submit" name="submit" value= "Add photos">
</td></tr>
</table>
</form>
</body>
</html>
__HTML_END;








?>

2 个答案:

答案 0 :(得分:0)

更改max_execution_time文件

php.ini的值
max_execution_time = 300

答案 1 :(得分:0)

如果你不能改变PHP.INI,那么PHP文件中的

set_time_limit(seconds)