我真的很难尝试使用php和mysql上传图片,我真的需要帮助!
这就是我想用自然语言做的事情:
1. upload an image
2. check if format is okay(png, jpeg, gif)
3. rename the image file, so theres no ambugity in this format(eg. pic-$userid.gif)
4. upload the pic to the images/folder
5. and delete the old one, or remove the defualt picture when on registration我正在尝试在线查找资源,但似乎找不到任何资源!!1谢谢:)
发布于 2010-10-23 03:32:00
如果你有耐心,我认为研究这些人的代码是最好的方法:http://www.verot.net/php_class_upload.htm
或者,为什么不直接使用他们的类。它会帮你从很多头痛中解脱出来,并帮助你更快地达到目标。
干杯!
发布于 2010-10-23 03:35:47
这里有一些来自我的Zend Framework控制器的灵感!也许还没有达到产品质量,但它已经完成了工作。
$location = $uploadForm->attachment->getFileName();
$image = new Imagick($location);
$size = $image->getImageGeometry();
$image->destroy();
$attachment = new Collection_Model_Attachment();
$attachment->memberId = Zend_Auth::getInstance()->getIdentity()->id;
$attachment->mimeType = $uploadForm->attachment->getMimeType();
$attachment->name = $uploadForm->attachment->getValue();
$attachment->width = $size['width'];
$attachment->height = $size['height'];
$attachment->sha1sum = sha1_file($location);
$attachmentMapper = new Collection_Model_AttachmentMapper();
$attachmentMapper->post($attachment);
$designAttachment = new Collection_Model_DesignAttachment();
$designAttachment->designId = $design->id;
$designAttachment->attachmentId = $attachment->id;
$designAttachmentMapper = new Collection_Model_DesignAttachmentMapper();
$designAttachmentMapper->post($designAttachment);
// Set default photo
if (!$design->attachmentId)
{
$design->attachmentId = $attachment->id;
$this->_designMapper->put($design);
}
$attachmentDir =
realpath(sprintf(
'%s/../data/attachments/%02d',
$_SERVER['DOCUMENT_ROOT'],
(int)($attachment->id / 100)
));
if (!file_exists($attachmentDir))
mkdir($attachmentDir);
$attachmentPath = sprintf('%s/%04d.jpg', $attachmentDir, $attachment->id);
$success = rename($location, $attachmentPath);
if (!$success)
{
// TODO: error handling
echo "move_uploaded_file failed: [$location] -> [$attachmentPath]";
$this->render('notfound');
return;
}https://stackoverflow.com/questions/4000252
复制相似问题