0
嗨,我很難用move_uploaded_file函數將文件傳遞到新的位置。我正在使用ajax將函數傳遞給我的process.php。 希望你能幫助我。如何上傳文件在服務器上使用move_uploaded_file
這裏是我的process.php
$target_dir = "../files-here/";
$filename = $_POST['filename'];
$path_url = $target_dir .$filename;
if (file_exists($path_url)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
else
{
if(move_uploaded_file($filename, $path_url))
echo "The file".$filename."has been uploaded";
這是我與阿賈克斯的形式。
echo "<form method='post' enctype='multipart/form-data' id='test_ajax'>";
echo "<input name='uploadedfile' type='file' id='custom-file-input' class='test-only' /><br/>";
echo "</form>";
echo '<button value="Upload" id="custom-submit-input">Upload</button>';
<script>
jQuery('#custom-submit-input').click(function(){
var filename = jQuery('input[type=file]').val().replace(/.*(\/|\\)/, '');
jQuery.post("file-here/process.php", {
filename: filename
}, function(data) {
console.log(data);
});
</script>
你只是發送文件名。您應該發送表單數據以上傳文件。看看這個http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload –