PHP File uploader. Cannot upload file to a folder - html

I made a PHP file uploader and I need to upload a file in a specific page of a webpage. I use Dreamweaver CS3 to make my websites and PHP files.
But there is a catch, my Manager doesn't know how to upload his Policies in a specific webpage. All policies are in PDF format. For that he wants me to make a PHP File Uploader to upload his files in front hand, for Example: There is a new policy which needs to be uploaded in a specific he/she would need to click on the choose file button, upload and choose the file, rename the following file and upload it in the webpage with ease.
It is easy to upload the Policies using Dreamweaver but my manager doesn't know how to upload files using that kind of method.
I need to make a php uploader, So i can upload my policies and post it in a specific page. How do you create a code for that.
EDIT
I have a html file by the name of uploads.html:
<form action="index1.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br><br>
<input type="submit" value="submit" name="submit">
</form>
and This is the php code (index1.php):
<?php
if(isset($_POST['submit'])){
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if(isset($name)){
if(!empty($name)){
$location = '../uploads/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'uploaded';
}
}
} else {
echo 'please uploaded';
}
}
?>
EDIT
The code for this uploader is for upload file in a specific folder, I need to upload my policies in a specific Webpage or any website. How can I create a code for this? I am sorry but I am new to PHP
I dont have enough reputation to post images so I will just insert the error statements here.
There are the following errors in my php file:
Warning: move_uploaded_file(../uploads/apache_pb2.gif): failed to open stream: No such file or directory in C:\xampp\htdocs\index1.php on line 8
Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpB959.tmp' to '../uploads/apache_pb2.gif' in C:\xampp\htdocs\index1.php on line 8

You are using a wrong function to perform upload operation.
Here you can find an answer to your problem:
http://php.net/manual/en/function.move-uploaded-file.php
There is an example of uploading a file. You can find more info in the users comments

Replace below line
copy( $_FILES['file']['name'], "/var/www/html" ) or
with
move_uploaded_file( $_FILES['file']['tmp_name'], "/var/www/html" ) or

$_FILES["file"]["tmp_name"] - the name of the temporary copy of the
file stored on the server
so You have to change Your code
move_uploaded_file( $_FILES['file']['tmp_name'], "/var/www/html/".$_FILES['file']['name'] )

$target_dir = $_SERVER['DOCUMENT_ROOT'] . URL_SUB_FOLDER . "/file/";
// move to folder file
$file = basename($_FILES["file"]["name"]);
$target_file0 = $target_dir . $file;
if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_file0)){
// do something.
}

Related

Can I have a link automatically download a file in an email?

I have an email with a link to a video file stored on a cloud hosting service (backblaze)
I'm trying to make it so that when someone can clicks the link the file would start to download. Right now I have this:
Download Here
That takes you to the video instead of downloading the file. I'd like to avoid that if possible and have the link prompt you to start downloading the file when you click on it.
Is this possible to do in an email when the file is stored on a server somewhere?
Thanks
I think you can't do this in plain html.
Since you can't use JavaScript in email, the best option would be to manage to include some PHP script in the server that do the job.
This example was taken from serverfault
PHP
?php
// We'll be outputting a MP4
header('Content-type: video/mp4');
// It will be called downloaded.mp4
header('Content-Disposition: attachment; filename="downloaded.mp4"');
// Add your file source here
readfile('original.mp4');
?>

Create download link for static asset file

I would like to put a download link on my website so the users can download the file template.csv. The file is static and is located at the root of my /grails-app/assets folder.
Inside my page.gsp, I have tried 2 methods do so, to no avail :
Download the template
this results in a template.csv file being downloaded, but the content is the html code of page.gsp rather than the original content of the file I uploaded in my assets.
Download the template
the generated html file has a link to localhost:8080/mysite/assets/template.csv but clicking it prompts an error message : Failed - no file.
What is the correct way to do what I want to achieve ? Is there an issue with extra permissions I would need to add to allow the download of my file ?
Our webapp relies on a rather old technological stack :
Grails 2.3.4
Plugin asset-pipeline-2.2.5
I have had some troubles with downloading files straight with a -tag.
To overcome this, I use Controller method to return the file and place the downloadable files in their own folder under web-app/:
class MyController {
#Autowired()
AssetResourceLocator assetResourceLocator
def downloadExcelTemplate () {
String fileName = "MyExcelFile.xlsx"
/* Note: these files are found in /web-app/downloadable directory */
Resource resource = assetResourceLocator.findResourceForURI("/downloadable/$fileName")
response.setHeader "Content-disposition", "attachment; filename=${resource.filename}"
response.contentType = 'application/vnd.ms-excel'
response.outputStream << resource.inputStream.bytes
}
}
And just use regular a-tag to to link to this controller method.
This way you also gain more control over file downloads.

Referring to a file with partially known name in html

I am making a webpage. I have to give link of a log file which is present on the server but that file is generated with a random number at the end.
I know the starting part of the filename. Here is my code -
Download log file Here
for example the name of the file is "log_parser2088.log" this 2088 number is randomly generated everytime the code runs and there is only one file in that folder which starts with the name "log_parser". I want to give reference to this file but this is not working.
HTML doesn't support this kind of references. Use PHP or ASP.NET for this task since these server side programms have access to the servers file system and can, f.e., read every filename in a directory.
Example for PHP:
// path to your log file directory
$directoryPath = "\\home\\logs\\";
// read all files from that directory
$files = scandir($directoryPath);
// print download link
foreach($path in $files)
echo("Download");

How to upload files from a plugin to wordpress media library?

I need to developed a front-end drag and drop plug in. First I collect the html5 drag and droping code.
Download the source code in zip format - https://github.com/Mashpy/html5-drag-and-drop-upload/archive/master.zip
Put the files in localhost and open. If you upload files it will be uploades in "uploads" folder. Because I put code in " /js/mashpy.js " that this time "upload.php" file will be loaded and it will send the files in "uploads" folder.
That means this process work something like this -
index.html -- js/mashpy.js -- upload.php -- send file to "uploads" -- dump.php will show the result that upload successfully.
Now I have to developed a front-end drag and drop plug in. But I don't know how to upload files in wordpress media library. In this source code upload.php works which send files to "uploads" folder. But what happened in wordpress? How to send files to wordpress media library?
You can't simply upload files to wp-content/uploads/. They won't show up in Media Library. You need to use WordPress function media_handle_upload($file,$post_id), passing 0 as post ID (so, it's not attached to any post/page).
Something like:
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, 0 );
}
}
You can study this plugin to see how it does it: Add From Server. And also research in WordPress Answers for examples of this function usage.

How to upload file by HTML only

<form action="upload.html" method="post" enctype="multipart/form-data">
<input type="file" />
<input type="submit" value="Upload" />
</form>
I saved this file as main.html
I made a dir called 'images' with chmod 777
what should be in the file called 'upload.html' ??
I don't want to use any php or asp files
just html files, so how can I do it by html files only ?
Client Side (Browser) => Web Server => Script => Server Manipulation
You CANNOT just use HTML files to upload to the server.
Web servers do serve static content (think 'GET') however
when it comes todynamic webpages or the ability for the client (browser)
to POST/PATCH content on the server you would need some sort
of script for that!
Choose perl, php, ruby, python, javascript, or whatever makes
you smile, and put that behind your webserver, with the appropriate
image uploading logic and you're good to upload pictures directly
to your server then :)
It's simple. You can't.
Call, main.php and have that contain code shown here to upload files to your server. It is fairly simple to do.
Your still going to need to use PHP/ASP somewhere. It's easier to utilize a single PHP file, but if you wanted your end users just to see an .html file instead, you could have it post to a php script file (but it must be a .php extension for the server to execute it).
As mentioned earlier, have a look at the W3 Schools post on PHP file uploading.
The simplest answer is no , you cant do it with the help of html . You need to use php or any server side language to handle file upload .
See this link : W3schools php file upload
Or you can use any other server side languages like asp , python , jsp etc...
An another possiblility is you can use javascript (client side) . Use javascript with ajax ..
See this link Click here