PHP,同时使用$\u文件和$\u POST
我在同时使用$\u文件和$\u POST时遇到了一个问题,因为我有一个表单可以上传一个图像和一些数据总线,当我使用其中一个时,它可以工作,但当我使用另一个id时,它就不工作了。 我的代码是:PHP,同时使用$\u文件和$\u POST,php,Php,我在同时使用$\u文件和$\u POST时遇到了一个问题,因为我有一个表单可以上传一个图像和一些数据总线,当我使用其中一个时,它可以工作,但当我使用另一个id时,它就不工作了。 我的代码是: <?php include 'debugging.php'; //phpinfo(); echo '<br />'; echo '<h1>Image Upload</h1>'; //create a form with a file upload co
<?php
include 'debugging.php';
//phpinfo();
echo '<br />';
echo '<h1>Image Upload</h1>';
//create a form with a file upload control and a submit button
echo <<<_END
<br />
<form method='post' action='uplaodex.php' enctype='multipart/form-data'>
Select File: <input type='file' name='picName' size='50' />
name: <input type='text' name='usName' size='50' />
username : <input type='text' name='usUsername' size='50' />
pass: <input type='password' name='usPass' size='50' />
email: <input type='text' name='usEmail' size='50' />
<br />
<input type='submit' value='Upload' />
<input type="hidden" name="submit" value="1" />
</form>
<br />
_END;
//above is a special use of the echo function - everything between <<<_END
//and _END will be treated as one large echo statement
//$_FILES is a PHP global array similar to $_POST and $_GET
if (isset($_FILES['picName'])and isset($_POST['submit'])) {
//we access the $_FILES array using the name of the upload control in the form created above
//
//create a file path to save the uploaded file into
$name = "images//" . $_FILES['picName']['name']; //unix path uses forward slash
//'filename' index comes from input type 'file' in the form above
//
//move uploaded file from temp to web directory
if (move_uploaded_file($_FILES['picName']['tmp_name'], $name)) {
// Create the file DO and populate it.
include 'Do_profile.php';
$file = new Do_profile();
//we are going to store the file type and the size so we get that info
$type = $_FILES['picName']['type'];
$size = $_FILES['picName']['size'];
$usName = trim($_POST['usName']);
$usUsername = trim($_POST['usUsername']);
$usPass = trim($_POST['usPass']);
$usEmail = trim($_POST['usEmail']);
$file->FileName = $name; //$name is initialised previously using $_FILES and file path
$file->FileSize = $size;
$file->Type = $type;
$file->usName = $usName;
$file->usUsername = $usUsername;
$file->usPass = $usPass;
$file->usEmail = $usEmail;
if ($file->save()) {
//select the ID of the image just stored so we can create a link
//display success message
echo "<h1> Thankyou </h1><p>Image stored successfully</p>";
//this above line of code displays the image now stored in the images sub directory
echo "<p>Uploaded image '$name'</p><br /><img src='$name' height='200' width='200'/>";
//create alink to the page we will use to display the stored image
echo '<br><a href="Display.php?id=' . $fileId . '">Display image ' .
$file->FileName . '</a>';
} else
echo '<p class="error">Error retrieving file information</p>';
}
else {
echo '<p class="error"> Oh dear. There was a databse error</p>';
}
} else {
//error handling in case move_uploaded_file() the file fails
$error_array = error_get_last();
echo "<p class='error'>Could not move the file</p>";
// foreach($error_array as $err)
// echo $err;
}
echo "</body></html>";
?>
如果要执行isset,必须验证文件的大小。我不知道这是否有效,但更好的方法是首先检查大小以验证是否已将isset或was发送到服务器
然后,在
中,您必须创建另一个名为uplaodex.PHP的PHP文件,并将数据发送到该文件。因此,您的代码应该与下面的代码类似,并考虑步骤1。这将是您的uploadex.php
$name_file = $_FILES['picName']['name'];
$type = $name_file['type'];
$size = $name_file['size'];
$tmp_folder = $name_file['tmp'];
$usName = trim($_POST['usName']);
$usUsername = trim($_POST['usUsername']);
$usPass = trim($_POST['usPass']);
$usEmail = trim($_POST['usEmail']);
if ( $size > 0 ) {
//REMOVE another slash images//
$name = "images/" . $name_file; //unix path uses forward slash
//'filename' index comes from input type 'file' in the form above
//
//move uploaded file from temp to web directory
if ( move_uploaded_file($tmp_folder, $name) ) {
// Create the file DO and populate it.
include 'Do_profile.php';
$file = new Do_profile();
$file->FileName = $name; //$name is initialised previously using $_FILES and file path
$file->FileSize = $size;
$file->Type = $type;
$file->usName = $usName;
$file->usUsername = $usUsername;
$file->usPass = $usPass;
$file->usEmail = $usEmail;
if ($file->save()) {
//USE PRINTF
printf('<h1> Thankyou </h1><p>Image stored successfully</p><br>
<p>Uploaded file: %s</p>. <img src="%s" height="200" width="200" />',
$name_file, $name );
#WHAT IS $fileId? In which moment was define?
//echo '<br><a href="Display.php?id=' . $fileId . '">Display image ' .
$file->FileName . '</a>';
}
else
echo '<p class="error">Error retrieving file information</p>';
}
else {
echo '<p class="error"> Oh dear. There was a databse error</p>';
} //ENDIF OF if (move_uploaded_file($_FILES['picName']['tmp_name'], $name))
} //ENDIF OF if ( $size > 0 )
#ELSE OF YOUR if ( $size > 0 )
else {
//error handling in case move_uploaded_file() the file fails
$error_array = error_get_last();
echo "<p class='error'>Could not move the file</p>";
// foreach($error_array as $err)
// echo $err;
}
$name\u file=$\u文件['picName']['name'];
$type=$name_文件['type'];
$size=$name_文件['size'];
$tmp_folder=$name_文件['tmp'];
$usName=修剪($_POST['usName']);
$usUsername=trim($_POST['usUsername']);
$usPass=修剪($_POST['usPass']);
$usEmail=trim($_POST['usEmail']);
如果($size>0){
//删除另一个斜杠图像//
$name=“images/”$name\u file;//unix路径使用正斜杠
//“filename”索引来自上表中的输入类型“file”
//
//将上载的文件从临时目录移动到web目录
如果(移动上传的文件($tmp\U文件夹,$name)){
//创建文件DO并填充它。
包括“Do_profile.php”;
$file=new Do_profile();
$file->FileName=$name;//$name以前使用$\u文件和文件路径初始化
$file->FileSize=$size;
$file->Type=$Type;
$file->usName=$usName;
$file->usUsername=$usUsername;
$file->usPass=$usPass;
$file->usEmail=$usEmail;
如果($file->save()){
//使用PRINTF
printf('Thankyou图像已成功存储
已上载文件:%s,
$name_文件,$name);
#什么是$fileId?在什么时候定义的?
//回声“
”;
}
其他的
echo“检索文件信息时出错
”;
}
否则{
echo'哦,天哪,数据库出现了错误;
}//如果的ENDIF(移动上传的文件($文件['picName']['tmp\U名称']],$name))
}//if的ENDIF($size>0)
#if的ELSE($size>0)
否则{
//如果移动\u上载的\u文件()文件失败,则进行错误处理
$error\u array=error\u get\u last();
echo“
无法移动文件”
;
//foreach($error\u数组为$err)
//echo$err;
}
如果要执行isset,必须验证文件的大小。我不知道这是否有效,但更好的方法是首先检查大小以验证是否已将isset或was发送到服务器
然后,在
中,您必须创建另一个名为uplaodex.PHP的PHP文件,并将数据发送到该文件。因此,您的代码应该与下面的代码类似,并考虑步骤1。这将是您的uploadex.php
$name_file = $_FILES['picName']['name'];
$type = $name_file['type'];
$size = $name_file['size'];
$tmp_folder = $name_file['tmp'];
$usName = trim($_POST['usName']);
$usUsername = trim($_POST['usUsername']);
$usPass = trim($_POST['usPass']);
$usEmail = trim($_POST['usEmail']);
if ( $size > 0 ) {
//REMOVE another slash images//
$name = "images/" . $name_file; //unix path uses forward slash
//'filename' index comes from input type 'file' in the form above
//
//move uploaded file from temp to web directory
if ( move_uploaded_file($tmp_folder, $name) ) {
// Create the file DO and populate it.
include 'Do_profile.php';
$file = new Do_profile();
$file->FileName = $name; //$name is initialised previously using $_FILES and file path
$file->FileSize = $size;
$file->Type = $type;
$file->usName = $usName;
$file->usUsername = $usUsername;
$file->usPass = $usPass;
$file->usEmail = $usEmail;
if ($file->save()) {
//USE PRINTF
printf('<h1> Thankyou </h1><p>Image stored successfully</p><br>
<p>Uploaded file: %s</p>. <img src="%s" height="200" width="200" />',
$name_file, $name );
#WHAT IS $fileId? In which moment was define?
//echo '<br><a href="Display.php?id=' . $fileId . '">Display image ' .
$file->FileName . '</a>';
}
else
echo '<p class="error">Error retrieving file information</p>';
}
else {
echo '<p class="error"> Oh dear. There was a databse error</p>';
} //ENDIF OF if (move_uploaded_file($_FILES['picName']['tmp_name'], $name))
} //ENDIF OF if ( $size > 0 )
#ELSE OF YOUR if ( $size > 0 )
else {
//error handling in case move_uploaded_file() the file fails
$error_array = error_get_last();
echo "<p class='error'>Could not move the file</p>";
// foreach($error_array as $err)
// echo $err;
}
$name\u file=$\u文件['picName']['name'];
$type=$name_文件['type'];
$size=$name_文件['size'];
$tmp_folder=$name_文件['tmp'];
$usName=修剪($_POST['usName']);
$usUsername=trim($_POST['usUsername']);
$usPass=修剪($_POST['usPass']);
$usEmail=trim($_POST['usEmail']);
如果($size>0){
//删除另一个斜杠图像//
$name=“images/”$name\u file;//unix路径使用正斜杠
//“filename”索引来自上表中的输入类型“file”
//
//将上载的文件从临时目录移动到web目录
如果(移动上传的文件($tmp\U文件夹,$name)){
//创建文件DO并填充它。
包括“Do_profile.php”;
$file=new Do_profile();
$file->FileName=$name;//$name以前使用$\u文件和文件路径初始化
$file->FileSize=$size;
$file->Type=$Type;
$file->usName=$usName;
$file->usUsername=$usUsername;
$file->usPass=$usPass;
$file->usEmail=$usEmail;
如果($file->save()){
//使用PRINTF
printf('Thankyou图像已成功存储
已上载文件:%s,
$name_文件,$name);
#什么是$fileId?在什么时候定义的?
//回声“
”;
}
其他的
echo“检索文件信息时出错
”;
}
否则{
echo'哦,天哪,数据库出现了错误;
}//如果的ENDIF(移动上传的文件($文件['picName']['tmp\U名称']],$name))
}//if的ENDIF($size>0)
#if的ELSE($size>0)
否则{
//如果移动\u上载的\u文件()文件失败,则进行错误处理
$error\u array=error\u get\u last();
echo“
无法移动文件”
;
//foreach($error\u数组为$err)
//echo$err;
}
if(isset($\u FILES['picName'])和isset($\u POST['submit'])
中的所有内容都不起作用,因为超全局$\u FILES
可能没有名为picName
的密钥。要检查这一点,请尝试var\u dump
-ing$\u文件
,如var\u dump($\u文件)代码>
通过var\u dump
的输出,您将了解$\u文件中是否有任何内容。如果已填充,只需查看密钥名称,然后使用该密钥访问该文件
但是如果数组是空的,那么在PHP或APACHE中可能存在一些错误配置
一个可能的修复方法是在ini文件中设置file\u uploads=On