PDA

View Full Version : hàm move_uploaded_file trong php



LUONG DINH
11-01-2009, 10:51
mình sử dụng hàm hàm move_uploaded_file trong php để chuyển file ảnh
File edit:


<?php
include 'config/connectdb.php';
if (isset($_GET['id']))
{
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM Categories WHERE CategoryID=$id") or die("Could not query database");
$row = mysql_fetch_array($result);

?>

<form id="add_pro" name="add_pro" method="post" action="functions/edit_category.php" class="keyview" enctype="multipart/form-data" onsubmit="return checkForm(categoryName)">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="maintable">
<tr>
<td class="fieldname">Tên loại sản phẩm</td>
<td><input type="text" name="categoryName" value="<?php echo $row['CategoryName']?>" />
(*) </td>
</tr>
<tr>
<td class="fieldname">Ảnh</td>
<td>
<?php echo "<img class='categoryimage' src='images/category/" . $row['Picture'] . "' border=0 />" ?>
<br />
Ảnh mới <input type="file" name="file" id="file" />
</td>
</tr>
<tr>
<td class="fieldname">Miêu tả</td>
<td><textarea name="description" cols="60" rows="4"><?php echo $row['Description']?></textarea></td>
</tr>
<tr>
<td class="fieldname">&nbsp;</td>
<td><input type="submit" name="Submit" value="Cập nhật" />
<input type="reset" name="Reset" value="Làm lại" />
(*) Thông tin cần thiết phải nhập </td>
</tr>
</table>
<input type="hidden" name="id" value="<?php echo $row['CategoryID']?>" />
</form>
<?php
}
?>

Sử dụng hàm move_uploaded_file :


<?php
include '../config/connect.php';
$NewFileName = '';
if (isset($_POST['ProductName']) && isset($_POST['id']))
{
$ProductPrice = $_POST['ProductPrice'];
$Materials = $_POST['Materials'];
$Origin = $_POST['Origin'];
$ProductName = $_POST['ProductName'];
$id = $_POST['id'];
$path = "../Images/"

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"];
}
else
{
$NewFileName = time( ) . $_FILES["file"]["name"];
if (file_exists($path . $NewFileName))
{
echo $NewFileName . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],$path . $NewFileName);
echo 'ban da lam dung';
}
}
}
}


Nhưng nó toàn chuyển ảnh đến thư mục function (Thư mục mà mình để hàm edit.php)

VuongChieuQuan
11-01-2009, 17:43
Chạy test thì bỏ hết những thứ linh tinh đi, để lại mỗi hàm move_uploaded_file để test thôi.

Code của bạn quá cẩu thả.

LUONG DINH
11-01-2009, 21:05
minh chang dc học bài bản lên trình bày code kém quá !
Bạn tham khảo cho mình cách viết code dễ nhìn, hay viết theo cái chuẩn gì ko ?

SVPro
12-01-2009, 09:28
Tự mình debug mấy cái linh tinh đi. Còn cái nào thật sự không làm được mới đưa lên đây. Đưa nguyên cả cái file với mấy cái linh tinh lung tung lên đây ma nào mà thèm đọc.

P/S: Hướng dẫn cho cách hỏi.