website : https://photo-gallery.000webhostapp.com/public/admin/photo_upload.php
unable to upload any images
my guess it’s permission problem but i didn’t understand why it it happening becuase i have set all permissions to “rwx” or “777” .
@a_nitw Can you paste this in your .htaccess and then try again
If that doesn’t help, can you post screenshot of error you’re getting??
Please lower your PHP version and try again: 000webhost cPanel > Settings > Change PHP version > 5.6
Also, fix files permissions: 000webhost cPanel > Settings > Fix files permission
If the problem persists, there is a bug in your PHP script.
ok i’ll try that !
can u tell me what is the scheme of uploading images on folder , did they go to directly on upload folder or first go to temp folder then upload folder , because i was using apache server that set the uploaded images on temp folder and we’ll have to moved images from there to upload folder
The scheme is same, first it uploads to temp path, then using “move_uploaded_file” you need to move the file to original upload directory.
Make sure you’ve given correct path.(as i believe this will be your issue)
ok ! i am doing same thing here’s the code , can u have a look and tell me error :
public function save(){
// A new record won't have an id yet.
if(isset($this->id)) {
// Really just to update the caption
$this->update();
} else {
// Make sure there are no errors
// Can't save if there are pre-existing errors
if(!empty($this->errors)) { return false; }
// Make sure the caption is not too long for the DB
if(strlen($this->caption) > 255) {
$this->errors[] = "The caption can only be 255 characters long.";
return false;
}
// Can't save without filename and temp location
if(empty($this->filename) || empty($this->temp_path)) {
$this->errors[] = "The file location was not available.";
return false;
}
// Determine the target_path
$target_path = SITE_ROOT .DS. 'public' .DS. $this->upload_dir .DS. $this->filename;
// Make sure a file doesn't already exist in the target location
if(file_exists($target_path)) {
$this->errors[] = "The file {$this->filename} already exists.";
return false;
}
// Attempt to move the file
if(move_uploaded_file($this->temp_path, $target_path)) {
// Success
// Save a corresponding entry to the database
if($this->create()) {
// We are done with temp_path, the file isn't there anymore
unset($this->temp_path);
return true;
}
} else {
// File was not moved.
$this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
return false;
}
}
}
can you check using this “is_uploaded_file”, that file is being uploaded to tmp directory or not??
i didn’t understand ! how to do so and where ?
We’ll need the entire class if we want to debug the code.
However please recheck:
$this -> temp_path
$target_path
i have tried that
entire class :
<?php
// if it's going to need the database , then it's probably smart choice to require it before start .
require_once(LIB_PATH.DS."database.php"); // to insure load once
class Photograph extends DatabaseObject {
protected static $table_name = "photographs";
protected static $db_fields = array('id','filename','type','size','caption');
public $id;
public $filename;
public $type;
public $size;
public $caption;
private $temp_path;
protected $upload_dir = "images";
public $errors = array();
protected $upload_errors = array(
UPLOAD_ERR_OK =>"No errors.",
UPLOAD_ERR_INI_SIZE =>"Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE =>"Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL =>"Partial upload.",
UPLOAD_ERR_NO_FILE =>"No file.",
UPLOAD_ERR_NO_TMP_DIR =>"No temporary directory.",
UPLOAD_ERR_CANT_WRITE =>"Can't write to disk.",
UPLOAD_ERR_EXTENSION =>"File upload stop by extension."
);
public function save(){
// A new record won't have an id yet.
if(isset($this->id)) {
// Really just to update the caption
$this->update();
} else {
// Make sure there are no errors
// Can't save if there are pre-existing errors
if(!empty($this->errors)) { return false; }
// Make sure the caption is not too long for the DB
if(strlen($this->caption) > 255) {
$this->errors[] = "The caption can only be 255 characters long.";
return false;
}
// Can't save without filename and temp location
if(empty($this->filename) || empty($this->temp_path)) {
$this->errors[] = "The file location was not available.";
return false;
}
// Determine the target_path
$target_path = SITE_ROOT .DS. 'public' .DS. $this->upload_dir .DS. $this->filename;
// Make sure a file doesn't already exist in the target location
if(file_exists($target_path)) {
$this->errors[] = "The file {$this->filename} already exists.";
return false;
}
// Attempt to move the file
if(move_uploaded_file($this->temp_path, $target_path)) {
// Success
// Save a corresponding entry to the database
if($this->create()) {
// We are done with temp_path, the file isn't there anymore
unset($this->temp_path);
return true;
}
} else {
// File was not moved.
$this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
return false;
}
}
}
public function destroy(){
//remove database entry
if ($this->delete()) {
//remove file
$target_path = SITE_ROOT.DS.'public'.DS.$this->image_path();
return unlink($target_path) ? true : false ;
}else{
// database delete failed
}
}
public function image_path(){
//we can change image dir easily and not going to affect any other php file
// just change upload_dir
return $this->upload_dir.DS.$this->filename;
}
public function size_as_text(){
if($this->size < 1024) {
return "{$this->size} bytes";
} elseif($this->size < 1048576) {
$size_kb = round($this->size/1024);
return "{$size_kb} KB";
} else {
$size_mb = round($this->size/1048576, 1);
return "{$size_mb} MB";
}
}
//pass $_FILE['upload_file']
public function attach_file($file){
if (!$file || empty($file) || !is_array($file)) {
$this->errors[] = "No file was uploaded.";
return false;
}elseif ($file['error']!=0) {
$this->errors[] = $this->upload_errors[$file['error']];
return false;
}else{
$this->temp_path = $file['tmp_name'];
$this->filename = basename($file['name']);
$this->type = $file['type'];
$this->size = $file['size'];
return true;
}
}
public function comments() {
return Comment::find_comments_on($this->id);
}
public function attributes(){
// return an array of attribute names and their values
$attributes=array();
foreach ( self::$db_fields as $field) {
if (property_exists($this, $field)) {
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
}
?>
@NGiNX
and here’s photo_upload.php :
<?php
require_once("../../includes/initialize.php");
if (!$session->is_logged_in()) { redirect_to("login.php");}
?>
<?php
$max_file_size = 1048576; // expressed in bytes\
if(isset($_POST['submit'])) {
$photo = new Photograph();
$photo->caption = $_POST['caption'];
$photo->attach_file($_FILES['file_upload']);
if($photo->save()) {
// Success
$page=0;
$per_page = 10;
$total_count = Photograph::count_all();
$session->message("Photograph uploaded successfully.");
$pagination = new Pagination($page,$per_page,$total_count);
redirect_to("list_photos.php?page=".$pagination->total_pages());
} else {
// Failure
$message = join("<br>", $photo->errors);
}
}
?>
<?php include_admin_layout_template("admin_header.php"); ?>
<a href="index.php">«Back </a>
<br>
<h2>Photo Upload</h2>
<?php echo output_message($message); ?>
<form action="photo_upload.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
<p><input type="file" name="file_upload" /></p>
<p>Caption: <input type="text" name="caption" value="" /></p>
<input type="submit" name="submit" value="Upload" />
</form>
<?php include_admin_layout_template("admin_footer.php"); ?>
@NGiNX
any help ?
I am working on it. There is definitely a path problem, but I don’t know where.
ok thanks !
Replace the entire code class with this one. The below code should upload the file in site root.
<?php
// if it's going to need the database , then it's probably smart choice to require it before start .
require_once(LIB_PATH.DS."database.php"); // to insure load once
class Photograph extends DatabaseObject
{
protected static $table_name = "photographs";
protected static $db_fields = array('id', 'filename', 'type', 'size', 'caption');
public $id;
public $filename;
public $type;
public $size;
public $caption;
public $temp_path;
protected $upload_dir = "images";
public $errors = array();
protected $upload_errors = array(
UPLOAD_ERR_OK =>"No errors.",
UPLOAD_ERR_INI_SIZE =>"Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE =>"Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL =>"Partial upload.",
UPLOAD_ERR_NO_FILE =>"No file.",
UPLOAD_ERR_NO_TMP_DIR =>"No temporary directory.",
UPLOAD_ERR_CANT_WRITE =>"Can't write to disk.",
UPLOAD_ERR_EXTENSION =>"File upload stop by extension.");
public function save()
{
if(is_uploaded_file($this -> filename))
{
// A new record won't have an id yet.
if(isset($this->id)) {
// Really just to update the caption
$this->update();
} else {
// Make sure there are no errors
// Can't save if there are pre-existing errors
if(!empty($this->errors)) { return false; }
// Make sure the caption is not too long for the DB
if(strlen($this->caption) > 255) {
$this->errors[] = "The caption can only be 255 characters long.";
return false;
}
// Can't save without filename and temp location
if(empty($this->filename) || empty($this->temp_path)) {
$this->errors[] = "The file location was not available.";
return false;
}
// Determine the target_path
//$target_path = SITE_ROOT .DS. 'public' .DS. $this->upload_dir .DS. $this->filename;
$target_path = SITE_ROOT . "/" . $this -> filename;
// Make sure a file doesn't already exist in the target location
if(file_exists($target_path)) {
$this->errors[] = "The file {$this->filename} already exists.";
return false;
}
// Attempt to move the file
if(move_uploaded_file($this->temp_path, $target_path))
{
// Success
// Save a corresponding entry to the database
if($this->create()) {
// We are done with temp_path, the file isn't there anymore
unset($this->temp_path);
return true;
}
} else {
// File was not moved.
$this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
return false;
}
}
}
else
{
echo "ERROR UPLOADING FILE";
}
}
public function destroy()
{
//remove database entry
if ($this->delete())
{
//remove file
$target_path = SITE_ROOT.DS.'public'.DS.$this->image_path();
return unlink($target_path) ? true : false ;
}
else
{
// database delete failed
}
}
public function image_path()
{
//we can change image dir easily and not going to affect any other php file
// just change upload_dir
return $this->upload_dir.DS.$this->filename;
}
public function size_as_text()
{
if($this->size < 1024)
{
return "{$this->size} bytes";
}
elseif($this->size < 1048576)
{
$size_kb = round($this->size/1024);
return "{$size_kb} KB";
}
else
{
$size_mb = round($this->size/1048576, 1);
return "{$size_mb} MB";
}
}
//pass $_FILE['upload_file']
public function attach_file($file){
if (!$file || empty($file) || !is_array($file)) {
$this->errors[] = "No file was uploaded.";
return false;
}
elseif ($file['error']!=0)
{
$this->errors[] = $this -> upload_errors[$file['error']];
return false;
}
else
{
$this->temp_path = $file['tmp_name'];
$this->filename = basename($file['name']);
$this->type = $file['type'];
$this->size = $file['size'];
return true;
}
}
public function comments() {
return Comment::find_comments_on($this->id);
}
public function attributes(){
// return an array of attribute names and their values
$attributes=array();
foreach (self::$db_fields as $field)
{
if (property_exists($this, $field))
{
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
}
?>
Hmm, I don’t know. Your code is too big for me to debug it. The issue can be caused by:
- Path problem
- Dependencies between properties/methods
My advice for you would be to re-write the entire code and keep it in a simple manner. In my opinion your code is a little fuzzy, that’s why it is hard to be debugged. Below I have provided a simple class which contains photo upload. It is original from one of my old projects. Maybe you can inspire from it.
##The class
class FILE
{
public $file;
public $path;
function __construct($a, $b)
{
$this -> file = $a;
$this -> path = $b;
}
function upload()
{
$finfo_resource = finfo_open();
$extension_temp = explode(".", $this -> file["name"]);
$extension_temp = end($extension_temp);
define("FILE_NAME", basename($this -> file["name"]));
define("FILE_SIZE", $this -> file["size"]);
define("FILE_TYPE", @finfo_file($finfo_resource, $this -> file["tmp_name"]));
define("FILE_EXTENSION", $extension_temp);
define("PATH_TO_MOVE", $this -> path);
finfo_close($finfo_resource);
if(
!strpos(FILE_TYPE, "JPEG") &&
!strpos(FILE_TYPE, "PNG") &&
!strpos(FILE_TYPE, "bitmap") &&
FILE_EXTENSION !== "jpg" &&
FILE_EXTENSION !== "jpeg" &&
FILE_EXTENSION !== "png" &&
FILE_EXTENSION !== "bmp"
)
{
die("<script>window.location = 'end.php?msg=Please upload only JPEG, PNG and BMP image formats.';</script>");
}
if(FILE_SIZE > 5000000)
{
die("<script>window.location = 'end.php?msg=File size too big. Please try again.';</script>");
}
$hashed_filename = hash("sha1", basename(FILE_NAME . time()));
if(move_uploaded_file($this -> file["tmp_name"], $this -> path . "/" . $hashed_filename . "." . FILE_EXTENSION))
{
print("<script>window.location = 'end.php?url=http://directimagehost.gq/" . "file.php?file=" . $hashed_filename . "." . FILE_EXTENSION . "';</script>");
}
else
{
print("<script>window.location = 'end.php?msg=Unknown error occured. Please try again later.';</script>");
}
}
function move()
{
!file_exists($this -> file) ? die("The provided combination 'path/file' does not exist.") : null;
!file_exists($this -> path) ? die("The provided path does not exist.") : null;
$filename_temp = explode("/", $this -> file);
$filename_temp = end($filename_temp);
define("FILE_NAME", $filename_temp);
if(rename($this -> file, $this -> path . "/" . FILE_NAME))
{
return "File moved with success!";
}
else
{
return "Unknown error occured.";
}
}
function delete()
{
!file_exists($this -> file) ? die("The provided combination 'path/file' does not exist.") : null;
if(unlink($this -> file))
{
return "File deleted with success!";
}
else
{
return "Unknown error occured." ;
}
}
}
##The upload engine
function upload()
{
!isset($_FILES["fileToUpload"]) ? die("No file supplied.") : null;
$upload = new FILE($_FILES["fileToUpload"], "uploads");
$upload -> upload();
}
I hope it will help you