I have a php page that lets you upload files, and the files just get uploaded to my computer, but I need them to be uploaded to a different hard drive. I need them to be uploaded to
"D:\uploads". I know there are some other problems with this code but i just need this out of the way. Heres the code itself:
<link rel="icon" type="image/png" href="favicon.ico.ico"/>
<?php
define("DOC_ROOT", $_SERVER['DOCUMENT_ROOT']."/");
define("PDF_UPLOADS", DOC_ROOT."uploads/");
#$name = $_FILES['file']['name'];
#$size = $_FILES['file']['size'];
#$type = $_FILES['file']['type'];
#$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name))
{
if (!empty($name))
{
if(move_uploaded_file($tmp_name, PDF_UPLOADS. $name))
echo 'Uploaded';
else
echo "Not Uploaded";
}
else
{
echo 'Please choose a file';
}
}
?>
<form action="first.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="Submit" value="Submit">
</form>
Wouldn't it be as simple as this:
define("PDF_UPLOADS", "D:/uploads/");
Related
hey guys so I created a website that you can upload books to and display them as a list I have a form to input the name of the book and the file type and I want them to display as the name and file type (on the same line) example (nameofabook epub) but when I try is display them it shows up like (nameofabook
new line epub) here's my code thank you
<?php
if (isset($_FILES['file']) && isset($_POST['name'])) {
$file = $_FILES['file'];
$file_name = $file['name'];
$file_type = $file['type'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
$file_ext = explode(".", $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array("mov", "avi", "mp4", "epub", "pdf"); //The extensions you allow
if (in_array($file_ext, $allowed)) {
if ($file_error === 0) {
if ($file_size <= 2097152) { //the maximum filesize
$file_destination = ''.$file_name; // If ' ', the file will be placed in this directory
if (move_uploaded_file($file_tmp, $file_destination)) {
echo $file_destination;
$fp = fopen('book_list.txt', "a");
fwrite($fp, $_POST['name']. "|||" .$file_destination."\n");
fwrite($fp, $_POST['type']. "|||" .$file_destination."\n");
fclose($fp);
} else {
echo "An error has been encountered while moving your file!";
}
} else {
echo "Your file is too big!";
}
} else {
echo "An error has been encountered while uploading your file!";
}
} else {
echo "You can't upload files of this type!";
}
}
?>
if anyones curious heres my html for the upload page
<!DOCTYPE html>
<html>
<head>
<title>Upload a Book</title>
<link href = "style2.css" type = "text/css" rel = "stylesheet" />
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name" required/>
Type: <input type="text" name="type" required/>
File: <input type="file" name="file" required/>
<input type="submit" value="Upload" />
</form>
</body>
</html>
Firstly...
Don't upload files directly in your site! You should upload them with an API, outside, in a cloud storage. In this case, you can use Cloudinary . So, you don't need to think much about the files uploaded. Again, you can show them easily!
Don't store information like names and others(without the file) in a text file. Storing them in a MySQL database is the best way to do so.
Secondly...
Use different inputs for collecting book names and other information so that, you can display and modify them easily.
Thirdly...
Please give the code you are using for displaying data
I'm a Homo sapiens So I can write anything wrong. Please forgive me for that.
This issue might have been discussed multiple times but I wanted a simple PHP script to upload a file, without any separate action file and without any checks. Below is my written code:-
<html>
<head>
<title>PHP Test</title>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="upload file" name="submit">
</form>
</head>
<body>
<?php echo '<p>FILE UPLOAD</p><br>';
$tgt_dir = "uploads/";
$tgt_file = $tgt_dir.basename($_FILES['fileToUpload']['name']);
echo "<br>TARGET FILE= ".$tgt_file;
//$filename = $_FILES['fileToUpload']['name'];
echo "<br>FILE NAME FROM VARIABLE:- ".$_FILES["fileToUpload"]["name"];
if(isset($_POST['submit']))
{
if(file_exists("uploads/".$_FILES["fileToUpload"]["name"]))
{ echo "<br>file exists, try with another name"; }
else {
echo "<br>STARTING UPLOAD PROCESS<br>";
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $tgt_file))
{ echo "<br>File UPLOADED:- ".$tgt_file; }
else { echo "<br>ERROR WHILE UPLOADING FILE<br>"; }
}
}
?>
</body>
</html>
I saved it in /var/www/html/phps/ location. But everytime I try to upload the file, I get ERROR WHILE UPLOADING FILE error. What am I doing wrong here. P.S. I have no previous experience of PHP, I just started with bits & pieces from internet.
Thanks
kriss
<?php
$target_dir = "uploads/";
$target_file = $target_dir .
basename($_FILES["fileToUpload"]["name"]);
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ".basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else {
echo "Sorry, there was an error uploading your file.";
}
?>
I hope that this thing will work and this is what you are in need of.
<?php
$name = $_POST['name'];
$image = $_FILES['fileToUpload']['name'];
$tempname = $_FILES['fileToUpload']['tmp_name'];
move_uploaded_file($tempname, "foldername/$image");?>
I want to upload video files in php for that i am using following code
PHP
$newUploadDir = "c://video";
$idx = "file";
if (isset($_FILES[$idx]) && is_array($_FILES[$idx])) {
echo "file set";
foreach ($_FILES[$idx]["error"] as $key => $error) {
echo "loop";
if ($error == UPLOAD_ERR_OK) {
echo "<br/>dd2";
$tmp_name = $_FILES[$idx]["tmp_name"][$key];
$name = $_FILES[$idx]["name"][$key];
$ext1 = explode(".", $name);
$extension = end($ext1);
$newfilename = "test".".".$extension;
$video_types = array('mp4', 'avi','webm');
if (in_array($extension, $video_types)) {
if (move_uploaded_file($tmp_name, $newUploadDir.$newfilename)) {
echo "uploaded to folder";
} else {
echo "Not uploaded to folder";
}
}
} else {
echo "not uploaded $error";
}
}
}
echo "ok";
HTML
<form action="http://localhost/fileupload/video.php" enctype="multipart/form-data" method="post">
<input id="file1" name="file[]" type="file"/>
<input name="userId" type="text" value="2"/>
<input id="Submit" name="submit" type="submit" value="Submit" />
</form>
Output
file setloopnot uploaded 1ok
Video file is not uploading. How to resolve this?
Actually, if you try to upload very large (video) files, probably the upload file size limit will not let you do that. Instead of regular file upload, there are other possibities. For the begining, look at this, this or this.
An other aproach would be to use third party services like Amazon S3, Microsoft Azure, etc.
I have xampp installed and I have a program that you choose a file and it gets uploaded to my server. I have a folder in htdocs called uploads thats meant for storing the pictures. When I upload them, it goes in the htdocs, but not the folder in htdocs i want. I did specify that i needed it to go there. Can someone help?
Heres the code:
<?php
#$name = $_FILES['file']['name'];
#$size = $_FILES['file']['size'];
#$type = $_FILES['file']['type'];
#$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name)) {
if (!empty($name))
{
$location = 'uploads/';
if (move_uploaded_file($tmp_name, $location. $name));
echo 'Uploaded';
}
else
{
echo 'Please choose a file';
}
}
?>
<form action="first.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="Submit" value="Submit">
</form>
Try below code:
Note: Make sure your uploads folder have write permission.
<?php
define("DOC_ROOT", $_SERVER['DOCUMENT_ROOT']."/");
define("PDF_UPLOADS", DOC_ROOT."uploads/");
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name))
{
if (!empty($name))
{
if(move_uploaded_file($tmp_name, PDF_UPLOADS. $name))
echo 'Uploaded';
else
echo "Not Uploaded";
}
else
{
echo 'Please choose a file';
}
}
?>
I'm using Azure server to open the website and linking to dropbox to store all the code files. I have an folder called 'image' but after uploading, though it shows "Uploaded!", I can't find that file.
Here's my code:
<?php
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if (isset($name)){
if(!empty($name)){
$location = 'image/';
if(move_uploaded_file($tmp_name, $location.$name)){
echo 'Uploaded!';
}else{
echo 'There was an error.';
}
}else{
echo 'Plz choose a file.';
}
}
?>
<form action="add.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="file" id="file">
<input type="submit" value="Upload Image" name="submit">
</form>
Your uploaded file is in the image folder that is in the same folder of your page.php.
See your code:
$location = 'image/';
if(move_uploaded_file($tmp_name, $location.$name)){
The final location of your file is $location.$name. This is image/your_file_name.your_extension