Did you upload a file from a form with a field named "file"?
Is "albert" the name of the user your web server is running as?
Did you really say chmod
or chown
?
upload_tmp_dir php
- Edited
I uploaded on file with one form
<?php
$info = "
<h1>Upload New jpg Images</h1>
<form method='post' action='imagelistindex.php?page=upload' enctype='multipart/form-data' >
<label>Find a jpg image to upload</label>
<input type='file' name='image-data' accept='image/jpeg'/>
<input type='submit' value='upload' name='new-image' />
</form>";
?>
upload_tmp_dir = /tmp
I reading one book for beginners in php
chown albert:albert /tmp
I want to send this file in this folder but i don't know how to do it
var/www/html/php8-for-absolute-beginners-main/ch4/imgs
put this PHP file on your web site and access it witha web browser. It should tell you what user your web server is running as. If you are on Debian or Ubuntu, it's probably www-data. If you are on CentOS or Red Hat or Fedora, it might be httpd or apache or apache2. If you have a module like suPHP installed, it might be albert or whatever.
<?php
passthru('whoami');
- Edited
bertrc
<input type='file' name='image-data' accept='image/jpeg'/>
So the name of the file form field is "image-data"...
There are so many problems here.
First, this command won't do anything
chmod albert:albert /tmp
chmod
changes the permissions mode of a directory or file to make it readable/writable/executable for user/group/everyone. Read the docs on it.
The /tmp
directory is a special directory on linux/unix/macos/bsd. You probably don't need to change any permissions on it. Try reading about it.
Changing the ownership of the /tmp directory to albert:albert is therefore probably a bad idea, so don't do this:
chown albert:albert /tmp
This code of youra assumes that you created a form file input with name="file"
:
echo "Filename: " . $FILES['file']['name']."<br>";
echo "Type : " . $FILES['file']['type'] ."<br>";
echo "Size : " . $FILES['file']['size'] ."<br>";
echo "Temp name: " . $FILES['file']['tmp_name'] ."<br>";
echo "Error : " . $FILES['file']['error'] . "<br>";
However, your HTML specifies a name of image-data`:
<input type='file' name='image-data' accept='image/jpeg'/>
Try reading the friendly documentation on handling file uploads. It's quite informative.
- Edited
<?php
$info = "
<h1>Upload New jpg Images</h1>
<form method='post' action='imageslistindex.php?page=imagesUpload' enctype='multipart/form-data' >
<label>Find a jpg image to upload</label>
<input type='file' name='image-data' accept='image/jpeg'/>
<input type='submit' value='upload' name='new-image' />
</form>";
?>
<?php
//complete code for index.php
$nav = "";
$info = "";
include_once "views/imageslistnavigation.php";
include_once "classes/Page_Data.class.php";
$pageData = new Page_Data();
$pageData->setTitle("Dynamic image gallery");
$pageData->setContent($nav);
$navigationIsClicked = isset($_GET['page']);
if ( $navigationIsClicked ) {
$fileToLoad = $_GET['page'];
} else {
$fileToLoad = "securelistgallery";
}
include_once "views/$fileToLoad.php";
$pageData->appendContent($info);
require "templates/page.php";
echo $page;
?>
<?php
$nav = "
<nav>
<a href='imagelistindex.php?page=securelistgallery'>Gallery</a>
<a href='imagelistindex.php?page=imageupload'>Upload new image</a>
</nav>
";
?>
<?php
//complete code for classes/Uploader.class.php
require_once "views/checkImageFile.php";
class ImagesUploader {
private $filename;
private $fileData;
private $destination;
private $keyValue;
//declare a constructor method
public function __construct( string $key ) {
$this->keyValue = $key;
$this->filename = $_FILES[$key]['name'];
$this->fileData = $_FILES[$key]['tmp_name'];
}
public function saveIn( $folder ) {
$this->destination = $folder;
}
public function save(){
$variableName = $this->keyValue;
$tmp = $_FILES[$this->keyValue]['tmp_name'];
$folderIsWriteAble = is_writable( $this->destination );
$notValid = checkImageFile($tmp, $variableName);
//if( $folderIsWriteAble ){
if( !$notValid and $folderIsWriteAble) {
$name = "$this->destination/$this->filename";
$success = move_uploaded_file( $this->fileData, $name );
} else {
$success = false;
}
return $success;
}
}
?>
<?php
//complete source code for views/upload.php
function upload(){
include_once "classes/ImagesUploader.class.php";
//image-data is the name attribute used in <input type='file' />
$uploader = new ImagesUploader( "image-data" );
$uploader->saveIn("imgs");
$fileUploaded = $uploader->save();
if ( $fileUploaded ) {
$out = "New file uploaded to Images Gallery";
} else {
$out = "Something went wrong file not uploaded to Images Gallery";
}
return $out;
}
$info = upload();
?>
@bertrc does it work?
- Edited
bertrc
I have this files but the images is not going in the folder imgs
ch4/views/imagesUploadForm.php
<?php
$info = "
<h1>Upload New jpg Images</h1>
<form method='post' action='imageslistindex.php?page=imagesUpload' enctype='multipart/form-data' >
<label>Find a jpg image to upload</label>
<input type='file' name='image-data' accept='image/jpeg'/>
<input type='submit' value='upload' name='new-image' />
</form>";
?>
ch4/imageslistindex.php
<?php
//complete code for index.php
$nav = "";
$info = "";
include_once "views/imageslistnavigation.php";
include_once "classes/Page_Data.class.php";
$pageData = new Page_Data();
$pageData->setTitle("Dynamic image gallery");
$pageData->setContent($nav);
$navigationIsClicked = isset($_GET['page']);
if ( $navigationIsClicked ) {
$fileToLoad = $_GET['page'];
} else {
$fileToLoad = "securelistgallery";
}
include_once "views/$fileToLoad.php";
$pageData->appendContent($info);
require "templates/page.php";
echo $page;
?>
ch4/views/imageslistnavigtion.php
<?php
$nav = "
<nav>
<a href='imagelistindex.php?page=securelistgallery'>Gallery</a>
<a href='imagelistindex.php?page=imageupload'>Upload new image</a>
</nav>
";
?>
ch4/classes/ImagesUploader.class.php
<?php
//complete code for classes/Uploader.class.php
require_once "views/checkImageFile.php";
class ImagesUploader {
private $filename;
private $fileData;
private $destination;
private $keyValue;
//declare a constructor method
public function __construct( string $key ) {
$this->keyValue = $key;
$this->filename = $_FILES[$key]['name'];
$this->fileData = $_FILES[$key]['tmp_name'];
}
public function saveIn( $folder ) {
$this->destination = $folder;
}
public function save(){
$variableName = $this->keyValue;
$tmp = $_FILES[$this->keyValue]['tmp_name'];
$folderIsWriteAble = is_writable( $this->destination );
$notValid = checkImageFile($tmp, $variableName);
//if( $folderIsWriteAble ){
if( !$notValid and $folderIsWriteAble) {
$name = "$this->destination/$this->filename";
$success = move_uploaded_file( $this->fileData, $name );
} else {
$success = false;
}
return $success;
}
}
?>
ch4/imageslistindex.php
<?php
//complete source code for views/upload.php
function upload(){
include_once "classes/ImagesUploader.class.php";
//image-data is the name attribute used in <input type='file' />
$uploader = new ImagesUploader( "image-data" );
$uploader->saveIn("imgs");
$fileUploaded = $uploader->save();
if ( $fileUploaded ) {
$out = "New file uploaded to Images Gallery";
} else {
$out = "Something went wrong file not uploaded to Images Gallery";
}
return $out;
}
$info = upload();
bertrc I have this files but the images is not going in the folder imgs
You have repeatedly posted your source code and made this complaint. You need to give a more detailed explanation of what happens. Do you see the form? Are you selecting a file and submitting the form? Does your server respond with any error message? Have you attempted to debug your code or have it write log entries or echo values to tell you what is happening?
- Edited
sneakyimp
it does not work
return "Something went wrong file not uploaded to Images Gallery
I check the folder imgs but the new images is not there it is not working
you can download the files and try if it is working
https://github.com/Apress/php8-for-absolute-beginners/tree/main
ch4
imageslistindex.php
Part of the problem here is that your code might fail quietly without writing a log or telling you what the problem is. I suspect that your web server does not have permission to write the destination folder. I suggest changing this line:
$folderIsWriteAble = is_writable( $this->destination );
To this so that it throws an exception:
$folderIsWriteAble = is_writable( $this->destination );
if (!$folderIsWriteAble) {
$user = exec('whoami'); // this may not work if your server has security locked down tightly
$msg = $this->destination . " is not writable by $user";
throw new Exception($msg);
}
I'm not sure how your server is configured so I don't know if it will display this exception to your browser when you access a page, but it should result in the exception being written to your server's error log. If you don't know where the error log is, change throw new Exception($msg)
to just die($msg)
If it says something like "imgs is not writable by www-data" then the problem is clearly that your apache user doesn't have permission to write your imgs folder. you would need to change permission on that folder so that the web server has permissiont to write it.