file uploading in php - php

Here my code
echo "Upload: " . $_FILES["audio"]["name"] . "<br/>";
echo "Type: " . $_FILES["audio"]["type"] . "<br/>";
echo "Size: " . ($_FILES["audio"]["size"] / 1024) . " kB<br/>";
echo "Temp file: " . $_FILES["audio"]["tmp_name"] . "<br/>";
move_uploaded_file($_FILES["audio"]["tmp_name"],
"/var/www/html/mnworld/upload/audiofile/" . $_FILES["audio"]["name"]);
echo "Stored in: " . "/var/www/html/mnworld/upload/audiofile/" . $_FILES["audio"]["name"];
I tried to upload file in this (/var/www/html/mnworld/upload/audiofile/) directory but its always empty its not showing any errors. I need to upload this directory

Check the permission of the folder where the files are to be stored is in 777 mode (both Read and write). If not, change the mode to chmod 777. And also make sure that the path to be stored should be correct.

Since you have the correct details. The problem is only the permission of the folder that unables you to move/copy the file.
Use this: chmod 777 /var/www/html/mnworld/upload/audiofile/

Related

Cannot upload using php scripts

I have a script in php that is used to upload files to a server. It was working first but i dont know why its not working again. It shows no error but the file is not still uploaded to the directory that i assigned to hold all uploaded files. Here is the part that takes care of the upload:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Uploaded: " . basename($_FILES["file"]["name"]) . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
//by default the size of the file is in bytes so u need to divide by 1024 in order to bring it to KB
echo "Temporarily stored in: " . $_FILES["file"]["tmp_name"] . "<br />";
$target="/file/" . basename($_FILES["file"]["name"]) . "<br />";
}
if(move_uploaded_file($_FILES["file"]["tmp_name"], $target))
{
echo "<h2>" . "The file has been stored on the server" . "<br />" . "<h2 />";
echo "New storage location is : " . '<a href="/public/files/" >' . $target . '</a>';
?>
<html>
<body>
<div align="right"><a href="/public/files/" >Uploaded files</a></div>
<br />
</body>
</html>
<?php
}
else
{
echo "<h2>" . "Error while saving the file to the server." . "<br />" . "File wont be found in the uploaded files directory" . "<br />" . "<h2 />";
echo "The error says: " . $_FILE["file"]["error"] . " What do we do now?" ;
echo"<pre>".print_r($_FILES,true)."</pre>";
?>
<?
/file/ is a directory in the root of your server's filesystem, which almost certainly doesn't exist. move_uploaded_file() works at the filesystem level and has absolutely NO awareness of your site's URI structure. You probably want something more like:
move_uploaded_file(...,. $_SERVER['DOCUMENT_ROOT'] . '/file/');
^^^^^^^^^^^^^^^^^^^^^^^^^^^---- add this
so that the file gets moved to a /file subdir of your site's root directory.

Moving Files with PHP that have been uploaded

I've gone through some of the questions, but haven't found an answer to my question so here it goes.
I've written a stereotypical script for uploading small files. The script works, and the file is uploaded to the server. However, I can't get it to upload it to move to the right subfolder.
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " MB<br>";
}
if (file_exists("/enhstudios/clients/media/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/enhstudios/clients/media/" . $_FILES["file"]["name"]);
}
?>
The error I get is:
Warning: move_uploaded_file(/enhstudios/clients/july.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients/fileuploadcode.php on line 20
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpkfxGLm' to '/enhstudios/clients/july.jpg' in /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients/fileuploadcode.php on line 20
Permissions for this directory are set at 777.
Where have I gone wrong? I've tried all different combos of subdirectories and still can't get it to upload to the right one.
Are you sure you don't mix the /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients and the /enhstudios/clients/ directories?
I think you would like to move to the longer one, but the second arguments first / makes it an absoulte path.
You can add there the full path, or you can try to remove the first /. (This relativisation works only if the document root is /hermes/waloraweb013/b1311/moo.enhstudios/)

PHP mkdir failing without errors

I've am trying to set up a small file sharing server on my home's local network and am running into some big problems with the uploader. the step it seems to be failing on is the directory creation step however, when I first posted this there were no errors in the Apache log files however, that turned out to be the result of a permission problem with the lag files.
this are the relevant log entries.
[Mon Mar 25 18:43:05 2013] [error] [client 10.0.0.17] PHP Warning: mkdir(): Permission denied in /server/upload_movie.php on line 10, referer: http://10.0.0.17/upload_movie.html
it confuses me because I have run
sudo chmod 0777 /server/*
sudo chmod 0777 /server
with /server/ being the rood directory.
my code is as follows
<?php
echo "starting". "<br>";
$allowedExts = array("mp4", "mpg", "avi", "mkv");
$extension = end(explode(".", $_FILES["uplodedfile"]["name"]));
echo "filetype parsed". "<br>";
$path = "/downloads/movies/unsorted/";
echo "checking upload directory". "<br>";
if(!is_dir($path)){
echo "upload directory not found, creating...";
if (mkdir($path,0777,true))
{
echo "directory creation complete". "<br>";
}
else
{
echo "directory creation failed at ".$path."<br>";
}
}
echo "checking file". "<br>";
if (false)
{
echo "filetype and size passed". "<br>";
if ($_FILES["uplodedfile"]["error"] > 0)
{
header('Location: upload_failure.php?file='.$_FILES["uplodedfile"]['name'].'&error='.$_FILES["uplodedfile"]["error"]);
exit();
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["uplodedfile"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists on server. ". "<br>";
}
else
{
echo "creating perminant copy of file". "<br>";
move_uploaded_file($_FILES["uplodedfile"]["tmp_name"],
$path."/" . $_FILES["uplodedfile"]["name"]);
echo "Stored in: " . "movie_uploads/" . $_FILES["file"]["name"]. "<br>";
}
}
header('Location: upload_success.php?type=movie');
exit();
}
else
{
echo "error:<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Name: " . $_FILES["file"]["name"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
echo "extension: ".$extension;
exit();
// echo "Invalid file";
}
?>
the output is as follows
starting
filetype parsed
checking upload directory
upload directory not found, creating...directory creation failed at /downloads/movies/unsorted/
checking file
error:
Type:
Name:
Size: 0 kB
Temp file:
extension:
and the code calling it is
<form enctype="multipart/form-data" action="upload_movie.php" method="POST">
<input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
i have tried all the suggestions in PHP mkdir: Permission denied problem and I have selinux turned off. I am using fedora 17. the server is being run on an ext4 partition which contains nothing else.
as was suggested in comments, i tried
$error = error_get_last(); echo $error['message'];
which yealded
Undefined index: file
You seem to have set the correct permissions to the /server-directory. But your server wants to write to the /downloads- directory. You should be up and running with this command:
Sudo chmod 777 /download
Your error message tells everything in it. This is file write permission problem. If you want to write your file in /downloads/movies/unsorted/ then you need to set write permission to this directory for all (777). But you are saying, you already checked permission for the downloads directory, but you might not set permission for recurring directories. So try below command before uploading file.
Sudo chmod -R 777 /download

retrieving image from database using php

<?php
$file= $_FILES["file"]["name"];//file selected in form
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
move_uploaded_file($_FILES["file"]["tmp_name"],
"c:/EasyPHP-12.1/www/new website/upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "c:/EasyPHP-12.1/www/new website/upload/" . $_FILES["file"]["name"];
if(($_FILES['file']['size'] >0))
{echo 'imagetrue';$con=mysql_connect('localhost','abc','YES') or die ("con");;
$db=mysql_select_db("website",$con) or die ("db");
$query=mysql_query("insert into website.picture (imagew) values ('$file')") or die('query'); //storing in db}
//echo $_FILES['file']['error'] ;
$query2= mysql_query("select * from website.picture");
$res=mysql_fetch_array($query2);
foreach($res as $row){
$str = "c:/EasyPHP-12.1/www/new website/upload/ ".$row['imagew'];
echo '<img src="'.$str.'" alt="no">' ;}
?>
i am using this script to store image and then displaying it on webpage but it is not working it only displays empty thumbnails...
I'd be very suspect about using absolute paths like c:/EasyPHP-12.1/www/new website/upload/
I'f you're then accessing the page from http://127.0.0.1/new-website (which I presume you are - or something similar) then having windows system pathnames could be a problem - do browsers even read the local filesystem like that ?
Also, it makes it very un-portable for when you move it to another server.
I'd suggest $str = 'upload/'.$row['imagew']; So it's relative to the document hosted in (I presume) 127.0.0.1/new website/image-viewer.php (or whatever you called it)
Check the return value of move_uploaded_file() - if it's false then it has failed. If that's failing try using the relative path :
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
This again presumes your script is sitting in 127.0.0.1/new-website
EDIT - NOTE ::: Is your form using enctype="multipart/form-data" ? If it's not then the images never get to the server !

Can't find uploaded files after PHP image upload

I'm uploading files from an iPhone app to PHP using the following code:
<?php
if ($_FILES["media"]["error"] > 0) {
echo "Error: " . $_FILES["media"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["media"]["name"];
echo "Type: " . $_FILES["media"]["type"];
echo "Size: " . ($_FILES["media"]["size"] / 1024);
echo "Stored in: " . $_FILES["media"]["tmp_name"];
if (file_exists("uploads/" . $_FILES["media"]["name"])) {
echo $_FILES["media"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["media"]["tmp_name"],
"uploads/" . $_FILES["media"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["media"]["name"];
}
}
?>
Afterwards, I get the success message saying "uploads/2542543.jpg" - but I can't seem to find where the image is actually stored. Is the filepath relative to the php file (which is in php - and has an uploads folder) or is it absolute from the root??
EDIT: Looks like the file should have ended up in php/uploads/filename.jpg - but it doesn't appear to be making it. I don't quite understand why - anyone have any idea?
This is an absolute path from the root
/uploads/2543.jpg
This is a relative path from your PHP document
uploads/2543.jpg
So you're working with a relative path (note the missing / at the start)
This is defined in php.ini using the upload_tmp_dir directive. If that's not set, it uses the system default, which you can figure out using sys_get_temp_dir. I believe it defaults to /tmp on Linux, and C:\Windows\Temp\ on Windows.

Categories