好的,我正在拉我的頭髮。我一整天都在努力讓這個工作正確無用。現在這個腳本正在做什麼,當你去上傳文件到頁面,沒有圖像。我不知道爲什麼它不能正常工作。我仍然在這個文件/圖像上傳的東西新。我嘗試了幾種不同的方式,而且什麼也沒做。這裏是upload.php的代碼:php中的文件上傳問題
<?php
function dbConnect(){
// Connect to the database
$hostname="localhost";
$database="myDatabase";
$mysql_login="myLogin";
$mysql_password="myPassword";
if(!($db=mysql_connect($hostname, $mysql_login, $mysql_password))){
echo"error on connect";
}
else{
if(!(mysql_select_db($database,$db))){
echo mysql_error();
echo "<br />error on database connection. Check your settings.";
}
else{
echo "This is the home page. I have successfully made a connection to my database and everything
is working as it should.";
}
}
$aryImages=array("image/jpeg","image/png");
$aryDocs=array("application/msword","application/pdf","video/x-msvideo");
$filename=filenameSafe($_FILES['upload']['name']);
$fileType=$_FILES["upload"]["type"];
if (in_array($_FILES["upload"]["type"],$aryImages)){
createThumb($fileType,$_FILES['upload']['tmp_name'],$filename,100,100);
}
elseif (in_array($_FILES["upload"]["type"],$aryDocs)){
move_uploaded_file($_FILES['upload']['tmp_name'],
"/home/valerie2/public_html/elinkswap/imagefolder/".$filename);
$aryColumns=array("sessionID"=>$curSess,"fileName"=>$filename,"fileType"=>$fileType,"thumbFileName"=>$thumbFilename,"dateCreated"=>date('Y-m-d H:i:s'));
dbInsert($filename,$aryColumns,$_FILES["upload"]["type"]);
}
else{
echo "File Uploaded";
}
}
function createThumb($type,$tmpname,$filename,$new_w,$new_h){
$thumbFilename="tmb-".$filename;
echo $type;
echo "<br>".$tmpname;
if (is_numeric(strpos($type,"jpeg"))){
$src_img=imagecreatefromjpeg($tmpname);
}
if (is_numeric(strpos($type,"png"))){
$src_img=imagecreatefrompng($tmpname);
}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (is_numeric(strpos($type,"jpeg"))){
imagejpeg($dst_img,"/home/valerie2/public_html/elinkswap/upload/".$thumbFilename);
imagejpeg($src_img,"/home/valerie2/public_html/elinkswap/upload/".$filename);
}
if (is_numeric(strpos($type,"png"))){
imagepng($dst_img,"/home/valerie2/public_html/elinkswap/upload/".$thumbFilename);
imagepng($src_img,"/home/valerie2/public_html/elinkswap/upload/".$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
dbInsert($filename,$thumbFilename,$type);
}
function filenameSafe($filename) {
$temp = $filename;
// Lower case
$temp = strtolower($temp);
// Replace spaces with a ’_’
$temp = str_replace(" ", "_", $temp);
// Loop through string
$result = "";
for ($i=0; $i<strlen($temp); $i++) {
if (preg_match('([0-9]|[a-z]|_|.)', $temp[$i])) {
$result = $result.$temp[$i];
}
}
dbConnect();
$SQL="SELECT fileID FROM upload WHERE fileName='".$result."'";
//echo $SQL;
$rs=mysql_query($SQL);
echo mysql_num_rows($rs);
if(mysql_num_rows($rs)!=0){
$extension=strrchr($result,'.');
$result=str_replace($extension,time(),$result);
$result=$result.$extension;
}
return $result;
}
function dbInsert($filename,$thumbFilename,$type){
dbConnect();
$SQL="INSERT Into upload (fileName,thumbFileName,fileType) values('".$filename."','".$thumbFilename."','".$type."')";
//echo $SQL;
mysql_query($SQL);
}
?>
,這是我的index.php代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>File Upload</title>
<link href="styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
Select File: <input type="file" name="upload">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
<input name="Submit" type="submit" value="Upload File">
</form>
</html>
這兩個文件的文件夾名稱imagefolder只有一個文件夾,名爲上傳裏面。我已經閱讀了很多關於文件/圖像和許多視頻的東西,但我仍然不明白爲什麼圖片不會顯示出來。我甚至試圖做另一種方式,但它一直告訴我invaild文件類型。
msg的錯誤是什麼? – Sedz
沒有錯誤信息,但沒有出現我試圖上傳的圖片。頁面是白色的。 – sn1984
什麼是我們的操作系統? – Sedz