PHP 图片上传 - 几个错误
PHP image upload - several errors
所以,我正在尝试使用 move_uploaded_file() 上传图片。这是代码:
HTML 表格:
<form method="post" action="?page=prod&action=register" enctype="multipart/form-data">
<fieldset class="field-prods">
<label>Name:</label> <br>
<input type="text" name="txtName">
</fieldset>
<fieldset class="field-prods">
<label>Price:</label> <br>
<input type="number" name="nmbPrice">
</fieldset>
<fieldset class="field-prods">
<label>Image:</label> <br>
<input type="file" name="fileImage">
</fieldset>
<button type="submit" class="btn-prods">Register</button>
</form>
PHP 脚本:
if ($_POST != null) {
if (!empty($_POST['txtName']) && !empty($_FILES['fileImage']) && !empty($_POST['nmbPrice'])) {
Product::insert($_POST['txtName'], $_FILES['fileImage'], $_POST['nmbPrice']);
$target = "img/prods/" . $_FILES['fileImage']['name'];
$fileTmpName = $_FILES['fileImage']['tmp_name'];
move_uploaded_file($fileTmpName, $target);
}
}
但是我收到了很多警告:
*Warning: Array to string conversion*
*Warning: move_uploaded_file(img/prods/livraria-print1.jpg): Failed to open stream: No such file or directory*
*Warning: move_uploaded_file(): Unable to move "F:\Programs\Xampp\tmp\php7CE5.tmp" to "img/prods/livraria-print1.jpg"*
有人可以帮我吗?
#数组转字符串
此警告在 insert
函数中:您传递的是数组 ($_FILES['fileImage']
) 而不是字符串。
看来你是想保存图片地址,如果是的话最好先保存图片再保存图片地址。
# move_uploaded_file(img/prods/livraria-print1.jpg): 无法打开流:...
这个警告很明确。检查您的目录并确保您使用的路径是正确的。
#move_uploaded_file(): 无法移动
而这个是因为上一个警告。
所以,我正在尝试使用 move_uploaded_file() 上传图片。这是代码:
HTML 表格:
<form method="post" action="?page=prod&action=register" enctype="multipart/form-data">
<fieldset class="field-prods">
<label>Name:</label> <br>
<input type="text" name="txtName">
</fieldset>
<fieldset class="field-prods">
<label>Price:</label> <br>
<input type="number" name="nmbPrice">
</fieldset>
<fieldset class="field-prods">
<label>Image:</label> <br>
<input type="file" name="fileImage">
</fieldset>
<button type="submit" class="btn-prods">Register</button>
</form>
PHP 脚本:
if ($_POST != null) {
if (!empty($_POST['txtName']) && !empty($_FILES['fileImage']) && !empty($_POST['nmbPrice'])) {
Product::insert($_POST['txtName'], $_FILES['fileImage'], $_POST['nmbPrice']);
$target = "img/prods/" . $_FILES['fileImage']['name'];
$fileTmpName = $_FILES['fileImage']['tmp_name'];
move_uploaded_file($fileTmpName, $target);
}
}
但是我收到了很多警告:
*Warning: Array to string conversion*
*Warning: move_uploaded_file(img/prods/livraria-print1.jpg): Failed to open stream: No such file or directory*
*Warning: move_uploaded_file(): Unable to move "F:\Programs\Xampp\tmp\php7CE5.tmp" to "img/prods/livraria-print1.jpg"*
有人可以帮我吗?
#数组转字符串
此警告在 insert
函数中:您传递的是数组 ($_FILES['fileImage']
) 而不是字符串。
看来你是想保存图片地址,如果是的话最好先保存图片再保存图片地址。
# move_uploaded_file(img/prods/livraria-print1.jpg): 无法打开流:...
这个警告很明确。检查您的目录并确保您使用的路径是正确的。
#move_uploaded_file(): 无法移动
而这个是因为上一个警告。