10/10/2018, 11:23
code upload hình ảnh lên server??
mình đang gặp rắc rối không biết làm thế nào, đoạn code upload của mình đây
session_start();
include("../connectDB.php");
include("../function.php");
if($user_id==1)
{
$update = mysql_query("select * from news where news_id='$id'") or die (mysql_error());
if ($submit<> "")
{
$hinhanh = isset($_FILES['hinhanh']['name'] ) ? $_FILES['hinhanh']['name'] : ';
if ( !empty($hinhanh) )
{
// GET THE TYPE OF IMAGE . EX: .GIF , .JPG ,....
$start = strpos($hinhanh,".");
$type = substr($hinhanh,$start,strlen($hinhanh));
if ( (strtolower($type)!=".gif")&&(strtolower($type)!=" .jpg"))
{
$msg = "B***7841;n ch***7881; ***273;***432;***7907;c upload file hình có d***7841;ng .gif ho***7863;c .jpg";
$page = "index.php?act=post";
page_transfer($msg,$page);
return false;
}
else
{
if (file_exists("./Picture/$hinhanh")) echo "<br>File $imgname is exist.<br>The new file will replace the old.";
if ( !(copy($_FILES['hinhanh']['tmp_name'], "../Pictures/" . $hinhanh)) ) die("Cannot upload file.");
}
}
$query = "update news set tieude ='". $tieude ."', hinhanh='". $hinhanh ."', trichdan='". $trichdan ."', noidung='". $noidung ."', chuthich='". $chuthich ."' where news_id = '$id'";
mysql_query($query) or die( mysql_error());
header("location: tintuc.php");
}
}
else
{
$msg = "B***7841;n ch***432;a ***273;***259;ng nh***7853;p ho***7863;c không có quy***7873;n truy c***7853;p vào trang này";
$page = "../index.php";
page_transfer($msg,$page);
return false;
header("localtion: index.php");
}
?>
test trên localhost thì chạy tốt, nhưng đưa lên host thi báo lổi:
Warning: copy(../pictures/chay 1.jpg): failed to open stream: No such file or directory in /home2/dalatgui/public_html/admin/updatenew.php on line 27
Cannot upload file.
có huynh nào biết giúp mình với!!
Cảm ơn nhiều
session_start();
include("../connectDB.php");
include("../function.php");
if($user_id==1)
{
$update = mysql_query("select * from news where news_id='$id'") or die (mysql_error());
if ($submit<> "")
{
$hinhanh = isset($_FILES['hinhanh']['name'] ) ? $_FILES['hinhanh']['name'] : ';
if ( !empty($hinhanh) )
{
// GET THE TYPE OF IMAGE . EX: .GIF , .JPG ,....
$start = strpos($hinhanh,".");
$type = substr($hinhanh,$start,strlen($hinhanh));
if ( (strtolower($type)!=".gif")&&(strtolower($type)!=" .jpg"))
{
$msg = "B***7841;n ch***7881; ***273;***432;***7907;c upload file hình có d***7841;ng .gif ho***7863;c .jpg";
$page = "index.php?act=post";
page_transfer($msg,$page);
return false;
}
else
{
if (file_exists("./Picture/$hinhanh")) echo "<br>File $imgname is exist.<br>The new file will replace the old.";
if ( !(copy($_FILES['hinhanh']['tmp_name'], "../Pictures/" . $hinhanh)) ) die("Cannot upload file.");
}
}
$query = "update news set tieude ='". $tieude ."', hinhanh='". $hinhanh ."', trichdan='". $trichdan ."', noidung='". $noidung ."', chuthich='". $chuthich ."' where news_id = '$id'";
mysql_query($query) or die( mysql_error());
header("location: tintuc.php");
}
}
else
{
$msg = "B***7841;n ch***432;a ***273;***259;ng nh***7853;p ho***7863;c không có quy***7873;n truy c***7853;p vào trang này";
$page = "../index.php";
page_transfer($msg,$page);
return false;
header("localtion: index.php");
}
?>
test trên localhost thì chạy tốt, nhưng đưa lên host thi báo lổi:
Warning: copy(../pictures/chay 1.jpg): failed to open stream: No such file or directory in /home2/dalatgui/public_html/admin/updatenew.php on line 27
Cannot upload file.
có huynh nào biết giúp mình với!!
Cảm ơn nhiều
Bài liên quan
P/S: nhìn code của bạn có chỗ này
f (file_exists("./Picture/$hinhanh")) echo "<br>File $imgname is exist.<br>The new file will replace the old.";
if ( !(copy($_FILES['hinhanh']['tmp_name'], "../Pictures/"
Một cái là ./ còn cái kia là ../ <~~~ nghĩa là 2 thằng khác nhau rồi, sau ở trên localhost vẫn chạy được nhỉ
{
if (file_exists("./Picture/$hinhanh")) echo "<br>File $imgname is exist.<br>The new file will replace the old.";
if ( !(copy($_FILES['hinhanh']['tmp_name'], "../Pictures/" . $hinhanh)) ) die("Cannot upload file.");
}
Có 2 vấn đề
1) Quan trọng là khi bạn name một cái file thì nên dùng một chữ liền với nhau ... Thí dụ: chay 1.jpg thì bạn nên để là chay1.jpg hoặc chay_1.jpg ... Không nên có chỗ trống ...
2) Trong cái script của bạn thiếu hoặc dư một cái chấm ngay chỗ ("./Picture/$hinhanh") và chữ s
Bạn nên sứa lại là như sau:
{
if (file_exists("./Pictures/$hinhanh")) echo "<br>File $imgname is exist.<br>The new file will replace the old.";
if ( !(copy($_FILES['hinhanh']['tmp_name'], "./Pictures/" . $hinhanh)) ) die("Cannot upload file.");
}
good luck,
MCL
<img src ="anh.jpg" >
Đây là link download http://bantot.net/web/tools.html