我正在使用PHP上传文件的表单。这些是我的意见:
$file_name = filter_input(INPUT_POST, 'file_name');
$file = filter_input(INPUT_POST, 'file');
$file_date = filter_input(INPUT_POST, 'premiere_date');
$file_director = filter_input(INPUT_POST, 'director_name');
这就是我不知道自己做错了什么:
if ((filter_input(INPUT_POST, 'submit'))) { //if submit button is clicked
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["file_name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
echo $target_file;
}
<form id="savingFiles" action="upload.php" method="post" enctype="multipart/form-data">
<label>Name of the file:</label>
<input type="text" name="file_name" value="<?php if (isset($row['file_name'])) {
echo $row['file_name'];
} ?>"/>
<br>
<label>Select your file:</label>
<input type="file" name="file" value="<?php if (isset($row['file'])) {
echo $row['file'];
} ?>"/>
<br>Date of premiere:
<input type="date" name="premiere_date" value="<?php if (isset($row['premiere_date'])) {
echo $row['premiere_date'];
} ?>"/>
<br>
<label>Name of the director:</label>
<input type="text" name="director_name" value="<?php if (isset($row['director_name'])) {
echo $row['director_name'];
} ?>"/>
<br>
<button type="submit" value="submit" name="submit">Upload file</button>
</form>
<?php
//Table with records
$sql = "SELECT myFilms.id, file_name, file, premiere_date, director_name FROM myFilms JOIN myFilms_directors ON myFilms.director_id = myFilms_directors.id";
/*for mysqlia
* $result = $conn->query($sql);
*/
/*for mysql (old)*/
$result = mysql_query($sql);
if ($result) {
echo "<table border='1' id='filesResults'><tr><th>FILE ID</th><th>FILE NAME</th><th>FILE</th><th>PREMIERE DATE</th><th>DIRECTOR NAME</th></tr>";
/* in mysqli
* while ($row = $result->fetch_assoc()) */
/*for mysql (old)*/
while ($row = mysql_fetch_array($result)) {
echo '<tr><td>' . $row['id'] . '</td><td>' . $row['file_name'] . '</td><td><a href= "'. $row['file']. ' ">view file</a></td><td>' . $row['premiere_date'] . '</td><td>' . $row['director_name'] . '</td></tr>';
}
echo "</table>";
} else {
echo "there is not data on the table";
}
/*for mysqli
$conn->close();
*/
/*for mysql (old)*/
mysql_close($dbhandle);
?>
所以,当我回显$ target_file时,我希望它是这个案例中的路径uploads / nameOfTheFile.pdf,但它只显示为uploads /所以$ target_dir,我使用的是basename函数吗?我已经检查了w3schools,这对我来说似乎是对的,但绝对肯定是错的......
谢谢你们!!
答案 0 :(得分:4)
改变
$ target_file = $ target_dir。基名($ _ FILES [ “文件”] [ “FILE_NAME”]);
到
$ target_file = $ target_dir。基名($ _ FILES [ “文件”] [ “名称”]);
应该有用。
答案 1 :(得分:1)
$ _ FILES是一个关联数组,包含:
Array ( [name] =>
[type] => some value
[tmp_name] => some value
error] => some value
[size] => some value
)
将$_FILES["file"]["file_name"]
更改为$_FILES["file"]["name"]
答案 2 :(得分:1)
使用public class GetLaboratoryListTask extends
AsyncTask<String, Integer, Object> {
private final Context mContext;
private final ProgressDialog mProgressDialog;
private ListView mlistView;
LaboratoryListModel labListModel;
public GetLaboratoryListTask(final Context mContext, ListView listView) {
this.mContext = mContext;
mlistView = listView;
mProgressDialog = new ProgressDialog(mContext);
mProgressDialog.setMessage("Please wait..");
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.setCancelable(false);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (mProgressDialog != null && !mProgressDialog.isShowing()) {
mProgressDialog.show();
}
}
@Override
protected Object doInBackground(String... params) {
LaboratoryListModel laboratoryListModel = (LaboratoryListModel) getLaboratoryList(params[0]);
if (laboratoryListModel != null)
return laboratoryListModel.getData();
else
return null;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
if (result != null) {
if (labListModel.getResponse().equalsIgnoreCase("true")) {
if (result != null && result instanceof List) {
mlistView.setAdapter(new SearchLabAdapter(
LabListActivity.this,
(List<LaboratoryList>) result));
}
} else {
}
}
}
private Object getLaboratoryList(String category_id) {
String webUrl = Constant.URL_SEARCH_LABORATORY;
try {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),
10000);
HttpResponse response;
JSONObject object = new JSONObject();
JSONObject objectContacts = new JSONObject();
try {
HttpPost post = new HttpPost(Constant.URL_SEARCH_LABORATORY);
objectContacts.put(Constant.CITY, "");
objectContacts.put(Constant.LOCATION_ZIPCODE, "");
objectContacts.put(Constant.LABORATORY_NAME, "");
StringEntity se = new StringEntity(
objectContacts.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post.setEntity(se);
response = client.execute(post);
if (response != null) {
//
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity()
.getContent(), "UTF-8"));
String json1 = reader.readLine();
JSONTokener tokener = new JSONTokener(json1);
jObject = new JSONObject(tokener);
}
} catch (Exception e) {
e.printStackTrace();
}
labListModel = (LaboratoryListModel) new Gson().fromJson(
jObject.toString(), LaboratoryListModel.class);
return labListModel;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
可以查看整个PHP var_dump($_FILES)
关联数组。正如您所见:
$_FILES
array(1) {
["file"]=> array(5) {
["name"]=> string(14) "plugin wp.docx"
["type"]=> string(71) "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
["tmp_name"]=> string(18) "/var/tmp/phpcoayaU"
["error"]=> int(0)
["size"]=> int(262453)
}
}
包含一个名为$_FILES
的&#39;文件&#39;它有5个索引:associative array
,name
,type
,tmp_name
和error
。
您正在分配:
size
由于$target_file = $target_dir . basename($_FILES["file"]["file_name"]);
不属于["file_name"]
,因此参考上述对象不正确。该解决方案应该明显地查看调试信息,只需将file
更改为["file_name"]
:
["name"]
有关PHP $target_file = $target_dir . basename($_FILES["file"]["name"]);
here的一些信息,因为您似乎没有意识到此功能。
答案 3 :(得分:0)
您输入的文件标记与名称字段关联为&#39;文件。&#39;
Instead of using $_FILES["file"]["file_name"] use this $_FILES["file"]["file"]
希望它对你有用。