如何上传和保存所需的名称的文件
我正在使用此代码上传文件(图像到文件夹)
<form action='' method='POST' enctype='multipart/form-data'> <input type='file' name='userFile'><br> <input type='submit' name='upload_btn' value='upload'> </form> <?php $target_Path = "images/"; $target_Path = $target_Path.basename( $_FILES['userFile']['name'] ); move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path ); ?>
当文件(图像)保存在指定的path…什么,如果我想用一些所需的名称保存文件….
我试图取代这个
$target_Path = $target_Path.basename( $_FILES['userFile']['name'] );
有了这个
$target_Path = $target_Path.basename( "myFile.png" );
但它不工作
你可以试试这个,
$info = pathinfo($_FILES['userFile']['name']); $ext = $info['extension']; // get the extension of the file $newname = "newname.".$ext; $target = 'images/'.$newname; move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
这将工作得很好 – 您可以使用HTML5来仅允许上传图片文件。 这是uploader.htm的代码 –
<html> <head> <script> function validateForm(){ var image = document.getElementById("image").value; var name = document.getElementById("name").value; if (image =='') { return false; } if(name =='') { return false; } else { return true; } return false; } </script> </head> <body> <form method="post" action="upload.php" enctype="multipart/form-data"> <input type="text" name="ext" size="30"/> <input type="text" name="name" id="name" size="30"/> <input type="file" accept="image/*" name="image" id="image" /> <input type="submit" value='Save' onclick="return validateForm()"/> </form> </body> </html>
现在代码为upload.php –
<?php $name = $_POST['name']; $ext = $_POST['ext']; if (isset($_FILES['image']['name'])) { $saveto = "$name.$ext"; move_uploaded_file($_FILES['image']['tmp_name'], $saveto); $typeok = TRUE; switch($_FILES['image']['type']) { case "image/gif": $src = imagecreatefromgif($saveto); break; case "image/jpeg": // Both regular and progressive jpegs case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break; case "image/png": $src = imagecreatefrompng($saveto); break; default: $typeok = FALSE; break; } if ($typeok) { list($w, $h) = getimagesize($saveto); $max = 100; $tw = $w; $th = $h; if ($w > $h && $max < $w) { $th = $max / $w * $h; $tw = $max; } elseif ($h > $w && $max < $h) { $tw = $max / $h * $w; $th = $max; } elseif ($max < $w) { $tw = $th = $max; } $tmp = imagecreatetruecolor($tw, $th); imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h); imageconvolution($tmp, array( // Sharpen image array(−1, −1, −1), array(−1, 16, −1), array(−1, −1, −1) ), 8, 0); imagejpeg($tmp, $saveto); imagedestroy($tmp); imagedestroy($src); } } ?>
你可以从这里获取演示源代码: http : //abhinavsingh.com/blog/2008/05/gmail-type-attachment-how-to-make-one/
它已准备好使用,或者可以修改以适应您的应用程序需求。 希望能帮助到你 :)
使用此目标path上传
<?php $file_name = $_FILES["csvFile"]["name"]; $target_path = $dir = plugin_dir_path( __FILE__ )."\\upload\\". $file_name; echo $target_path; move_uploaded_file($_FILES["csvFile"]["tmp_name"],$target_path. $file_name); ?>
configuration“php.ini”文件
首先,确保PHP被configuration为允许file upload。 在你的“php.ini”文件中,searchfile_uploads指令,并将其设置为On:
file_uploads = On
创buildHTML表单
接下来,创build一个HTML表单,允许用户select他们想要上传的图像文件:
<!DOCTYPE html> <html> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> </form> </body> </html>
上面的HTML表单的一些规则:确保表单使用method =“post”表单还需要以下属性:enctype =“multipart / form-data”。 它指定提交表单时使用哪种内容types。如果没有上述要求,file upload将不起作用。 其他要注意的事项:标记的type =“file”属性将input字段显示为文件select控件,在input控件旁边具有“浏览”button上述表单将数据发送到名为“upload.php “,我们将在下一步创build。
创build上传文件PHP脚本
“upload.php”文件包含上传文件的代码:
<?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } ?>
这里是PHP代码上传数据到数据库,显示并保存到本地文件夹
-
在第一个HTML代码forms:
<div class="upload"> <form method="POST" enctype="multipart/form-data" id="imageform"> <br> <input type="file" name="image" id="photoimg" > <br><br> <input type="submit" name="submit" value="UPLOAD"> </form> </div>
-
这里是整个PHP代码:
(只需要2个字段)在表中, id(INT) 255 primary key AUTO INCREMENT and your image row(anyname) (MEDIUMBLOB)
创build数据库和表
<?php if(isset($_POST['submit'])){ if(@getimagesize($_FILES['image']['tmp_name']) == FALSE){ echo "<span class='image_select'>please select an image</span>"; } else{ $image = addslashes($_FILES['image']['tmp_name']); $name = addslashes($_FILES['image']['name']); $image = file_get_contents($image); $image = base64_encode($image); saveimage($name,$image); $uploaddir = 'profile/'; //this is your local directory $uploadfile = $uploaddir . basename($_FILES['image']['name']); echo "<p>"; if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {// file uploaded and moved} else { //uploaded but not moved} echo "</p>"; } } displayimage(); function saveimage($name,$image) { $con = mysql_connect("localhost","root","your database password"); mysql_select_db("your database",$con); $qry = "UPDATE your_table SET your_row_name='$image'"; $result = @mysql_query($qry,$con); if($result) { echo "<span class='uploaded'>IMAGE UPLOADED</span>"; } else { echo "<span class='upload_failed'>IMAGE NOT UPLOADED</span>"; } } function displayimage() { $con = mysql_connect("localhost","root","your_password"); mysql_select_db("your_database",$con); $qry = "select * from your_table"; $result = mysql_query($qry,$con); while($row = mysql_fetch_array($result)) { echo '<img class="image" src="data:image;base64,'.$row[1].'">'; } mysql_close($con); } ?>
- 上传的PDF文件
- 如何获得所选文件的完整path改变使用javascript,jquery-ajax的<input type ='file'>?
- Java的Selenium WebDriver使用Java Robot API进行file upload的一个解决scheme
- 将PHP(/ PHP-FPM / Apache)的临时上传文件存储在RAM中而不是文件系统(或仅encryption)?
- HTML5file upload到Java Servlet
- Android文件select器
- 如何使用jQuery序列化file upload
- 在Codeigniter中上传多个文件
- 使用FileUpload Control获取文件的完整path