Php 即使在使用move_upload_file()之后,文件也未上载?
实际上,我正在尝试向db发送一个图像文件。而不是图片,我试图发送到图片链接…在数据库中的值是非常好,但有一个问题…文件没有上传到指定的文件夹Php 即使在使用move_upload_file()之后,文件也未上载?,php,Php,实际上,我正在尝试向db发送一个图像文件。而不是图片,我试图发送到图片链接…在数据库中的值是非常好,但有一个问题…文件没有上传到指定的文件夹 $file_name=$_FILES['images']['name']; $file_tmp =$_FILES['images']['tmp_name']; $heading=$_POST['heading']['name']; $headline=$_POST['headlines']['name'];
$file_name=$_FILES['images']['name'];
$file_tmp =$_FILES['images']['tmp_name'];
$heading=$_POST['heading']['name'];
$headline=$_POST['headlines']['name'];
$story=$_POST['story']['name'];
$date=date("m/d/Y");
$random = substr(md5(mt_rand()), 0, 13);
$moved= move_uploaded_file($file_tmp,"images/".md5(date('dYm')).$file_name);
$url="images/".md5(date('dYm')).$file_name;
$query="INSERT INTO news(news_id,pic,heading,headline,story,date)
VALUES('$random','$url','$heading','$headline','$story','$date')";
$data=mysqli_query($conn,$query);
if( $moved ) {
echo "Successfully uploaded";
} else {
echo "Not uploaded because of error #".$_FILES["images"]["error"];
}
这是我的ajax
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: new FormData(this),
dataType: 'json',
contentType: false,
cache: false,
processData:false,
success: function(response){
alert(response);
if(response.error){
alert(response.error);
}
}
});
我再说一遍,数据插入到数据库中是绝对正确的。问题在于图像上传中。
尝试:移动上传的文件($file\u tmp,getcwd()。“/images/”。
您可以接受sql注入。请,您已经准备好了语句。不,没有help@freeek你怎么能引导我一点呢?@UMARSTACK不是我,而是我能。
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
ob_clean();
# A directory, somewhere, to save files to...
$dir='c:/temp/fileuploads/1/';
/* The file field name simply for convenience */
$fieldname='images';
/* default value for uploaded file if there is a problem */
$obj=false;
if( !empty( $_FILES[ $fieldname] ) ){
/* standard file upload */
$obj=(object)$_FILES[ $fieldname ];
$obj->method='FILES'; # For illustrative porpoises only
} elseif( !empty( file_get_contents( 'php://input' ) ) ){
/* file upload via other means */
$obj=file_get_contents( 'php://input' );
$obj->method='PHP://INPUT'; # For illustrative porpoises only
}
if( $obj ){
/* extract properties of uploaded file into variables */
$name=$obj->name;
$tmp=$obj->tmp_name;
$error=$obj->error;
$type=$obj->type;
$size=$obj->size;
$method=$obj->method;
/* If all is OK, do interesting things */
if( $error == UPLOAD_ERR_OK ){
/* Move the file to it's final location, run SQL query */
$targetpath=$dir . $name;
$status=move_uploaded_file( $tmp, $targetpath );
if( $status ){
//$sql='insert into `news` (f1,f2,f3,f4) values (?,?,?,?)'; # etc
//$stmt=$db->prepare( $sql ); # etc
//$stmt->bind_param('ssss',$v1,$v2,$v3,$v4); # etc
//$stmt->execute(); # etc
}
}
}
$json=json_encode( $obj ); // here only to illustrate workings
exit( $json );
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>File upload</title>
<script>
let d=document;
d.addEventListener('DOMContentLoaded',()=>{
d.querySelector( 'form > input[ type="button" ]' ).addEventListener('click',e=>{
const ajax=function(url,params){
const success_callback=function(r){
if( this.readyState==4 && this.status==200 ){
console.info( r );// do more interesting things here than this!!!
}
};
const error_callback=function(e){
alert( e )
};
let xhr=new XMLHttpRequest();
xhr.onload=success_callback;
xhr.onerror=error_callback;
xhr.open( 'POST', url, true );
xhr.send( params );
};
ajax( location.href, new FormData( document.forms.geronimo ) );
});
});
</script>
</head>
<body>
<form name='geronimo' method='post' enctype='multipart/form-data'>
<input type='file' name='images' />
<input type='button' value='Upload image and do other stuff' />
</form>
</body>
</html>