我真的很难尝试使用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
我试图在网上寻找资源,但cnt似乎找到了任何!! 1谢谢:))
答案 0 :(得分:2)
这里有一些来自我的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;
}
答案 1 :(得分:1)
如果你有耐心,我认为调查这些人代码是最好的方法:http://www.verot.net/php_class_upload.htm
或者,为什么不,只是使用他们的课程。它可以帮助您避免很多头痛,并帮助您更快地实现目标。
干杯!