我使用本教程http://www.allphptricks.com/upload-file-using-php-save-directory/在PHP中设置表单和代码以上传视频文件。它表示每次成功上传,但是当我检查没有存储视频文件时
HTML CODE
<html>
<head>
<title>Upload File Using PHP and Save in Directory - AllPHPTricks.com</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>
<body>
<br /><br />
<form name="form" method="post" action="upload.php" enctype="multipart/form-data" >
<input type="file" name="my_file" /><br /><br />
<input type="submit" name="submit" value="Upload"/>
</form>
<br /><br />
<a href="http://www.allphptricks.com/upload-file-using-php-save-directory/">Tutorial Link</a> <br /><br />
For More Web Development Tutorials Visit: <a href="http://www.allphptricks.com/">AllPHPTricks.com</a>
</body>
</html>
PHP代码
<?php
if (($_FILES['my_file']['name']!="")){
// Where the file is going to be stored
$target_dir = "upload/";
$file = $_FILES['my_file']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['my_file']['tmp_name'];
$path_filename_ext = $target_dir.$filename.".".$ext;
// Check if file already exists
if (file_exists($path_filename_ext)) {
echo "Sorry, file already exists.";
}else{
move_uploaded_file($temp_name,$path_filename_ext);
echo "Congratulations! File Uploaded Successfully.";
}
}
?>
就像我说的,它报告文件已成功上传,但实际上并未存储和上传任何内容。
答案 0 :(得分:0)
是的,该文件可能已上传,但您无法检查是否已存储该文件。成功时move_uploaded_file
返回true,失败时返回false http://php.net/manual/en/function.move-uploaded-file.php,因此您需要检查它是否返回任何错误。
问题可能与您尝试在
中存储文件的文件夹的写访问有关答案 1 :(得分:0)
我发现使用绝对路径而不是相对路径效果更好 - 它可能在* nix系统上有所不同,但肯定在Windows上是真的。
<?php
$fieldname='my_file';
if( isset( $_FILES[ $fieldname ] ) ){
$target_dir = $_SERVER['DOCUMENT_ROOT'] . '/upload/';
$file = $_FILES[ $fieldname ]['name'];
$tmp = $_FILES[ $fieldname ]['tmp_name'];
$filename = pathinfo( $file, PATHINFO_FILENAME );
$targetpath = $target_dir . $filename;
if( !file_exists( $target_dir ) ){
exit('Error: Target directory does not exist');
}
clearstatcache();
if( file_exists( $targetpath ) ) {
echo 'Sorry, file already exists.';
}else{
$results = move_uploaded_file( $tmp, $targetpath );
echo $results ? 'Congratulations! File Uploaded Successfully.' : 'Error-Failed to store file';
}
clearstatcache();
}
?>