Errors at image after set my web on public - php

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

Related

Upload doesn't do anything when I hit the upload button

This is my first question here, I hope I can explain my problem. I have PHP code where I want to upload a PDF file and select a date, then upload it to a folder in my project called "pdfs". Then I want to download it but only until the date I chose at the upload is reached.
My upload doesn't work and I don't know why, here is my code:
<?php
include_once 'headeradmin.php';
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/styleUpload.css">
<title>Umleitung hochladen</title>
</head>
<body>
<div class="container">
<div class="row">
<form action="umleitungen.php" method="post" enctype="multipart/form-data">
<h3>Umleitungen hochladen</h3>
<input type="file" name="fileToUpload" id="fileToUpload" accept="application/pdf"><br>
<p> Datum: <input type="date" name="ablaufdatum"></p> <br>
<button type="submit" name="save">Hochladen</button>
</form>
</div>
</div>
</body>
</html>
<?php
if (isset($_POST['submit'], $_POST['ablaufdatum']) && is_uploaded_file($_FILES['fileToUpload']['tmp_name'])) {
$date = strtotime($_POST['ablaufdatum']);
if (false === $date) {
return;
}
$mimeType = mime_content_type($_FILES['fileToUpload']['tmp_name']);
$allowedFileTypes = ['application/pdf'];
if (!in_array($mimeType, $allowedFileTypes, true)) {
return;
}
$destination = 'pdfs/' . date('Y-m-d', $date) . '_' . time() . '.pdf';
if (!file_exists($destination)) {
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $destination)) {
echo 'Supi :)';
} else {
echo 'Upload fehlgeschlagen :/';
}
}
}
include_once 'footer.php';```
Check that the web server has permissions to write to the "pdfs/" directory.

Upload multiple images returning false

Trying to upload multiple images to a folder choose from a drop down list. I managed to make it work perfectly but all of a sudden it stopped working. The move_uploaded_file() function returns false. I have no idea why. I used echo to print out path and its 100% correct. I also checked destination directory permissions (set to 777).
I'm no expert in php, I followed multiple tutorials and guides to come up with the below code.
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8">
<title>Welcome</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/component.css" />
<link rel="icon" href="/MobileApp/Portal/favicon.png">
<script>(function(e,t,n){var r=e.querySelectorAll("html")[0];r.className=r.className.replace(/(^|\s)no-js(\s|$)/,"$1js$2")})(document,window,0);</script>
<style type="text/css">
body{ font: 14px sans-serif; padding-left:15px; padding-top:15px; text-align: center;}
</style>
</head>
<body>
<?php
if(isset($_POST['submit']) & isset($_POST['s1'])){
$choice = $_POST['s1'];
$fixedPath = 'MobileApp/Images/';
$slash = "/";
// Count total files
$countfiles = count($_FILES['file']['name']);
// Looping all files
for($i=0;$i<$countfiles;$i++){
$filename = $_FILES['file']['name'][$i];
$chosenPath = $fixedPath.$choice.$slash.$i;
//$chosenPath = $fixedPath.$choice.$slash.$filename;
echo $chosenPath;
// Upload file
if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $chosenPath)){
echo "Good";
}
else {
echo "bad";
echo $FILES['files']['error'];
}
//move_uploaded_file($_FILES['file'],$chosenPath/.$_FILES['tmp_name']);
}
}
else {
echo "Please choose a folder";
}
?>
<p>
<legend>Files Upload</legend>
<div class="box">
<form method='post' action='' enctype='multipart/form-data' style="text-align: center">
<input type="file" name="file[]" id="file" class="inputfile inputfile-1" data-multiple-caption="{count} files selected" multiple />
<label for="file">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17">
<path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z" />
</svg>
<span>Choose a fileā€¦</span>
</label>
</div>
<input type='submit' name='submit' value='Upload' class="btn btn-warning" padding-top="15px" style="width:500px">
<BR>
<BR>
<fieldset>
<p>
<label>User's folder:</label>
<form method='post' action='' name='s1'>
<select name="s1" style="width:400px">
<option value="" selected="selected">-----</option>
<?php
foreach(glob(dirname( dirname( dirname( __FILE__ ) ) ) . DIRECTORY_SEPARATOR . 'Images' . DIRECTORY_SEPARATOR . '*') as $filename){
//foreach(glob(dirname(__FILE__) . '/MobileApp/Images/') as $filename){
$filename = basename($filename);
echo "<option value='" . $filename . "'>".$filename."</option>";
}
?>
</select>
</form>
</p>
</fieldset>
</form>
</p>
</body>
</html>
UPDATE:
I have done some tweaking in my code and narrowed down the issue to this line of code:
move_uploaded_file($_FILES['file']['tmp_name'][i], $chosenPath)
but can't see my issue..
I think the only change is needed here
$chosenPath = $fixedPath.$choice.$slash.$filename;
instead of
$chosenPath = $fixedPath.$choice.$slash.$i;
the second argument for the move_uploaded_file should be a destination filename. Only that one change worked for me.
One more thing that you need to provide the destination directory path from the root directory, that means $fixedPath = 'MobileApp/Images/'; this should be a path from root directory like /var/www/html/../../MobileApp/Images/ (it's only an example your actual directory path can be different) check this link to find the relative path of the destination directory

Wamp issue for testing upload PHP file

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

How to upload a file to a specific folder using 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.

Video file uploading

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.

Categories