gpt4 book ai didi

image - 蛋糕PHP图片上传

转载 作者:行者123 更新时间:2023-12-01 07:26:17 25 4
gpt4 key购买 nike

我是蛋糕 PHP 的新手,我需要一些上传图片的帮助。我需要允许用户上传图像,我希望该图像保存在目录中(www/CakePHP/app/webroot/img/),并且还希望数据库存储图像的文件路径。这是我到目前为止所得到的

产品ADD.ctp:

<div class="products form">
<?php echo $this->Form->create('Product'); ?>


<fieldset>
<legend><?php echo __('Add Product Details'); ?></legend>
<?php
echo $this->Form->input('product_name', array('required'=>false));
echo $this->Form->input('product_model_number', array('required'=>false));
echo $this->Form->input('product_brand', array('required'=>false));
echo $this->Form->input('product_description', array('required'=>false));
echo $this->Form->input('price_bronze', array('required'=>false));
echo $this->Form->input('price_silver', array('required'=>false));
echo $this->Form->input('price_gold', array('required'=>false));
echo $this->Form->input('upload', array('type'=>'file'));


?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>

产品 Controller :
   public function add() {
if ($this->request->is('post')) {
$this->Product->create();
if ($this->Product->save($this->request->data)) {
$this->Session->setFlash(__('The product has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
}
if(!empty($this->data))
{
//Check if image has been uploaded
if(!empty($this->data['products']['upload']['name']))
{
$file = $this->data['products']['upload']; //put the data into a var for easy use

$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions

//only process if the extension is valid
if(in_array($ext, $arr_ext))
{
//do the actual uploading of the file. First arg is the tmp name, second arg is
//where we are putting it
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'CakePHP/app/webroot/img/' . $file['name']);

//prepare the filename for database entry
$this->data['products']['product_image'] = $file['name'];
}
}

//now do the save
$this->products->save($this->data) ;
}
}

}

我似乎无法弄清楚为什么上传的图像没有保存到目录中。有人可以帮忙吗。

最佳答案

在带有文件输入的 cakephp 中创建新表单时,您必须设置 'enctype'=>'multipart/form-data'

代码应该是这样的:

<?php echo $this->Form->create('Product',array('enctype'=>'multipart/form-data')); ?>

我希望这会奏效

关于image - 蛋糕PHP图片上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23353568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com