Php 使用zend框架上传图像
我是zend框架的新手。我需要创建一个图像上传用户注册。我已经做到了。我的注册控制器和上传控制器工作。目前,所有图像都正在上载到公用文件夹中的Php 使用zend框架上传图像,php,zend-framework,image-uploading,Php,Zend Framework,Image Uploading,我是zend框架的新手。我需要创建一个图像上传用户注册。我已经做到了。我的注册控制器和上传控制器工作。目前,所有图像都正在上载到公用文件夹中的uploads/user images/。但我的实际需求是,我必须根据每个用户的用户ID为他们创建一个特定的文件夹,并需要将他们的照片上传到该文件夹。此外,我还需要将该图像路径存储在DB中,作为实际名称\u当前时间戳\u宽度\u高度.扩展名,但我不知道如何执行此操作。我在这里包括我的控制器代码: <?php /** * PhotoControlle
uploads/user images/
。但我的实际需求是,我必须根据每个用户的用户ID为他们创建一个特定的文件夹,并需要将他们的照片上传到该文件夹。此外,我还需要将该图像路径存储在DB中,作为实际名称\u当前时间戳\u宽度\u高度.扩展名
,但我不知道如何执行此操作。我在这里包括我的控制器代码:
<?php
/**
* PhotoController class file
*
* This file contains a class for PhotoController
*
* @category Zend
* @package Zend_Controller
*
*/
class PhotoController extends Daddara_Controller_BaseController
{
public $session;
function init()
{
parent::init();
$this->view->headScript()->appendFile('/resources/js/upload.js');
$this->view->headScript()->appendFile('/resources/js/jquery.min.js');
$this->view->headScript()->appendFile('/resources/js/jquery.wallform.js');
$this->view->headScript()->appendFile('/resources/js/modernizr.custom.js');
// $this->view->headScript()->appendFile('/resources/js/lightbox/jquery-1.10.2.min.js');
// $this->view->headScript()->appendFile('/resources/js/lightbox/lightbox-2.6.min.js');
}
function indexAction()
{
$this->_forward('upload');
}
/**
* Upload Action action
*
*/
public function uploadAction()
{
$path = "uploads/user-images/";
$shortPath = "uploads/user-images/resize/";
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) {
return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$em = $this->getEntityManager();
//User Id
$userId = $em->getRepository("Models\User")->findOneBy(array('id' => $this->currentUser->id));
//Registration Id
$registrationId = $em->getRepository("Models\Registration")->findOneBy(array('user' => $this->currentUser->id));
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if($this->getRequest()->isPost()){
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
$ext = getExtension($name);
//echo $ext;exit;
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $ext), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
if($ext=="jpg" || $ext=="jpeg" )
{
$uploadedfile = $path.$actual_image_name;
$src = imagecreatefromjpeg($uploadedfile);
}
else if($ext=="png")
{
$uploadedfile = $path.$actual_image_name;
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=160;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "uploads/user-images/resize/". $actual_image_name;
imagejpeg($tmp,$filename,100);
$photos = new Models\ProfilePhotos();
$photos->setUser($userId)
->setRegistration($registrationId)
->setPhoto($path.$actual_image_name)
->setShortImage($shortPath.$actual_image_name)
->setAddedOn();
$em->persist($photos);
$em->flush();
echo "<img src='/uploads/user-images/resize/".$actual_image_name."' class='preview'>";
}
else
echo "Fail upload folder with read access.";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
}
}
public function listAction()
{
$em = $this->getEntityManager();
$photo = $em->getRepository('Models\ProfilePhotos')->findBy(array('user' => $this->currentUser->id));
//print_r($image);exit;
$this->view->photo = $photo;
}
/**
* delete function
*/
function deleteAction()
{
$deleteId = $this->getRequest()->getParam('id');
if(empty($deleteId)){
throw new Zend_Controller_Exception("Page not found", 404);
}
$em = $this->getEntityManager();
$del = $em->find("Models\ProfilePhotos", $deleteId);
$em->remove($del);
$em->flush();
$this->addMessage('Data Deleted Successfully "');
$this->_redirect('/photo/list');
}
}
您可以使用以下命令创建文件夹:
您可以使用和移动文件:
我已经尝试过下面的代码,这对我很有用。现在,我可以根据每个用户的用户id将图像上载到特定文件夹。希望这对其他人有所帮助:-)
这可能会有帮助:@v.eigler:非常感谢您的帮助..但该链接不包含我要求的任何必要信息..当我尝试使用此链接时,它将通知显示为未定义变量:userdir。。实际上,我在$userId中获取用户id,如果该目录不存在,我需要使用它来创建该目录,并且需要将该用户照片存储在此目录中,并在db中存储指向该图像的链接。您可以将变量名称更改为首选名称。但是如果您在实现解决方案时遇到问题,您应该阅读更多的php和zend framework教程。谢谢..我已经给出了一些类似的东西,我不知道这种方法是否正确。。其显示mkdir()权限被拒绝$userId=$em->getRepository(“Models\User”)->findOneBy(array('id'=>$this->currentUser->id));$user_path='$path.$userId';如果(!file_存在($user_path))mkdir($user_path);
if(mkdir(APPLICATION_PATH . '/public/uploads/' . $userId)){
//dir created
}
if(copy(APPLICATION_PATH . '/public/uploads/' .$filename,
APPLICATION_PATH . '/public/uploads/' .$userId .'/' .$newfilename)) {
unlink(APPLICATION_PATH . '/public/uploads/' . $filename);
}
$path = "uploads/user-images/";
$em = $this->getEntityManager();
//User Id
$userId = $em->getRepository("Models\User")->findOneBy(array('id' => $this->currentUser->id));
$user_path = "$path".$userId->getId()."/";
if (!file_exists($user_path)) mkdir($user_path, 0777);