I'm trying to learn how to upload by using Wamp server..
But unsure is it the settings or my code.
Please enlighten me.
Upload.htmlS
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="http://localhost/testing/php/file_uploader.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
The uploader.php
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], "C:\wamp\www\beta\images" ) or
die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
The error i've received is ,
Warning: copy(bannerbelow.JPG): failed to open stream: No such file or directory in C:\wamp\www\beta\admin\php\file_uploader.php on line 4
This works for me:
<?php
if( $_FILES['file']['name'] != "" ) {
$uploadfolder = $_SERVER['DOCUMENT_ROOT']."/beta/images";
$filename = $_FILES['file']['name'];
move_uploaded_file( $_FILES['file']['tmp_name'], "$uploadfolder/$filename" ) or
die( "Could not copy file!");
}
else {
die("No file specified!");
}
?>
$path = 'C:\wamp\www\beta\images\'; // make sure dir is exist
if(!is_dir($path)){
mkdir($path)
}
//write code for move file via copy or move_uploaded_file funcition
Related
I got errors after setup my website on public and when I finished I tried to upload the image and get errors. Is this because my website doesn't use database?
Warning: move_uploaded_file(uploads/Capture.PNG): failed to open stream: No such file or directory in /storage/ssd5/873/2077873/public_html/upload.php on line 28
Warning: move_uploaded_file(): Unable to move '/storage/ssd5/873/2077873/tmp/phpuRC82y' to 'uploads/Capture.PNG' in /storage/ssd5/873/2077873/public_html/upload.php on line 28
My code:
<html>
<head>
<title>image sharer</title>
<style>
footer {
font-family: sans-serif;
position:absolute;
bottom:0;
width:100%;
}
</style>
</head>
<body>
<h1><font color="green">#img</font><font color="blue">serv</font></h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
image selector :<input type="file" name="image"><br/><br/>
<input type="submit" name="upload" value="upload">
</form>
<?php
if(isset($_POST['upload'])){
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name= $_FILES['image']['tmp_name'];
#$desc = $_POST['desc'];
move_uploaded_file($image_tmp_name,"uploads/$image_name");
echo "<img src='uploads/$image_name' width='400' height='250'><br>";
echo "your image : ";
}
?>
<footer>
tos - faq - contact us
<span id="footerRight" class="copyright footerFont"><strong>#imgserv v1.1 </strong>
</span>
</div>
</footer>
</body>
</html>
The problems are not caused by the fact your website does not use a database.
The error message does point out that the elected upload directory cannot be found. Before attempting to write to that directory it would be better to test that it exists and if it does not create the necessary file structure. The function createdir below will recusively create the folder structure.
The upload handling code referenced a non-existant form field desc so you would likely have found an issue with that also.
<?php
function createdir( $path=null, $perm=0644 ) {
if( !file_exists( $path ) ) {
createdir( dirname( $path ) );
mkdir( $path, $perm, true );
clearstatcache();
}
}
$uploadstatus = false;
if( isset( $_POST['upload'], $_FILES['image'] ) ){
$obj=(object)$_FILES['image'];
$name=$obj->name;
$tmp=$obj->tmp_name;
$size=$obj->size;
$error=$obj->error;
$type=$obj->type;
if( $error == UPLOAD_ERR_OK && is_uploaded_file( $tmp ) ){
$target_directory = $_SERVER['DOCUMENT_ROOT'] . '/uploads';
$target_file = $target_directory . DIRECTORY_SEPARATOR . $name;
if( !file_exists( $target_directory ) ){
createdir( $target_directory );
}
$uploadstatus = move_uploaded_file( $tmp, $target_file );
}
}
?>
<html>
<head>
<title>image sharer</title>
<style>
footer {
font-family: sans-serif;
position:absolute;
bottom:0;
width:100%;
}
</style>
</head>
<body>
<h1><font color="green">#img</font><font color="blue">serv</font></h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Image selector :<input type="file" name="image"></label><br/>
<label>Description: <input type='text' name='desc' /></label><br/>
<input type="submit" name="upload" value="upload">
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && $uploadstatus==true ){
echo "
<img src='/uploads/{$name}' width='400' height='250' />
<br />
Your image : {$name} - {$_POST['desc']}";
}
?>
</form>
<footer>
tos -
faq -
contact us
<span id="footerRight" class="copyright footerFont"><strong>#imgserv v1.1 </strong></span>
</div>
</footer>
</body>
</html>
uploads folder is missing in your directory..please create uploads directory in your website folder
Guide me to find the number of characters which are present in the text file which i upload.
This code will display the characters which are present in the .txt file`
<!DOCTYPE html>
<html>
<head>
<title>File Reader</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="60" accept=".txt" />
<input type="submit" value="Show Contents" />
</form>
<?php
if ($_FILES) {
$fileName = $_FILES['file']['tmp_name'];
$file = fopen($fileName, "r");
while (!feof($file)) {
echo fgets($file) . "";
}
while (!feof($file)) {
echo fgetc($file);
}
fclose($file);
}
?>
</body>
</html>
You could use
$_FILES['userfile']['size']
this give you the size, in bytes, of the uploaded file.
http://www.php.net/manual/en/features.file-upload.post-method.php
My code is as follows..Here when I upload a file named Koala.jpg,then the warning is shown--Warning: copy(Koala.jpg): failed to open stream: No such file or directory in E:\xampp\htdocs\Forum\upload.php on line 4
Could not copy file!..Please solve the problem urgently..
Select a file to upload: <br />
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
***upload.php***
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], "/uploads" ) or
die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
Try this
<?php
if( $_FILES['file']['name'] != "" ) {
$path=$_FILES['file']['name'];
$pathto="/uploads/".$path;
move_uploaded_file( $_FILES['file']['tmp_name'],$pathto) or die( "Could not copy file!");
}
else {
die("No file specified!");
}
?>
Source :
http://php.net/manual/en/features.file-upload.post-method.php
http://www.tizag.com/htmlT/htmlupload.php
Replace copy function by following
$destFile = "/root/mysite/upload_files/".$_FILES['file']['name'];
move_uploaded_file( $_FILES['file']['tmp_name'], $destFile );
Note the location is a filesystem destination not a URL. Also the filesystem path is relative to the highest root rather than the website home URL.
I'm new to php and I followed a tutorial that shows how to upload a video file.
At this moment it uses move_uploaded_file function but it doesn't work, the file is not shown in "videos" folder. Can somebody explain to me why the file isn't showing up?
<html>
<head>
<title>Video Upload System</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
include "connect.php";
?>
<div id='box'>
<form action="index.php" method="POST" enctype="multipart/form-data">
<?php
if(isset($_FILES['video'])){
$name = $_FILES['video']['name'];
$type = explode('.', $name);
$type = end($type);
$size = $_FILES['video']['size'];
$random_name = rand();
$tmp = $_FILES['video']['tmp_name'];
if($type != 'mp4' && $type != 'MP4' && $type != 'flv'){
$message = "Video Foramt Not Supported!";
}else{
move_uploaded_file($tmp, 'videos/'.$random_name.'.'.$type);
$message = "Successfully Uploaded";
}
echo "$message <br/><br>";
}
?>
Select Video: <br/>
<input type='file' name='video' />
<br/><br/>
<input type='submit' value='Upload' />
</form>
</div>
<div id='box'>
<?php
?>
</div>
</body>
</html>
you can check if uploaded successfully..
if(move_uploaded_file($tmp, 'videos/'.$random_name.'.'.$type)) {
$message = "Successfully Uploaded";
}
I suspect the path you have provided is not valid, tough.
I think that you need to check the php.ini file to see the upload file (video) size limit and increase it, or just try to upload a small size file.
I followed every tutorial in the internet that I find to upload a file. But still, It failed. It gave me this error:
Warning: move_uploaded_file(/var/www/projects/upload/TASK.txt): failed to open stream: No such file or directory in /var/www/projects/test/upload.php on line 6 Warning: move_uploaded_file(): Unable to move '/tmp/phpjr2JJA' to '/var/www/projects/upload/TASK.txt' in /var/www/projects/test/upload.php on line 6 Something went wrong
index.html
<head>
<title></title>
</head>
<body>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<input type="file" name="upload" ><br />
<input type="hidden" name="MAX_FILE_SIZE" value="1024" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
upload.php
<?php
$target_path = $_SERVER['DOCUMENT_ROOT'] . "/upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );
if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
echo "has been uploaded";
} else {
echo "Something went wrong";
}
Can you help me and point out where I went wrong? I'm using ubuntu 12.04 and also I tried to change the permission for the /upload folder to 755 and checked the file_upload in php.ini is ON
Any help would be much appreciated. Thanks!
$_SERVER['DOCUMENT_ROOT'] gives /var/www/projects/upload.. as output
/ in the start is cause of error
Hence
try with relative path
<?php
$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );
if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
echo "has been uploaded";
} else {
echo "Something went wrong";
}
?>
worked for me
File & Dir permissions...
Add in your FTP Program 666 to the files and dirs what you need to write.
I think ur file upload location incorrect
index.php
<html>
<head>
<title>Upload your file</title>
</head>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="Upload" value="Submit">
</form>
</body>
upload.php
<?php
$target="give the path you want to store the file/";
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$target. $_FILES["file"]["name"]);
}
?>