I am having issues with my file uploads script this is the function(jp_upload_pic()) the are i am having issues specifically is the move_uploaded() function, The second parameter "destination" this is my value ("../usr/ceo/JPCEO_20") but this error it is telling me.
Warning: move_uploaded_file(../usr/ceo/JPCEO_20): failed to open stream: No such file or directory in /var/www/jobinpal/includes/func.php on line 1115 Warning: move_uploaded_file(): Unable to move '/tmp/phpgf1k7y' to '../usr/ceo/JPCEO_20' in /var/www/jobinpal/includes/func.php on line 1115
function jp_upload_pic()
{
if (isset($_GET['upload'])) {
$jp_img=$_FILES['file_to_be_uploaded'];
// file uploader for jobinpal...
$jp_img_name=$jp_img['name'];// name of the file
$jp_img_type=$jp_img['type'];
$jp_img_size=$jp_img['size'];
$jp_img_tmp_name=$jp_img['tmp_name'];//$_FILES['userfile']['tmp_name']
$jp_img_err=$jp_img['error'];// the error of the file...
// check if the image post isset before working...
if (isset($jp_img)) {
//do the string of the file name to get the .xxx extension
$jp_img_name_len=strlen($jp_img_name);
$jp_img_ext=substr($jp_img_name ,-4, $jp_img_name_len );
//check the image type of the image
$jp_img_size;
if (
/* file must be any of this format else bounce */
(
($jp_img_ext == '.jpg') ||
($jp_img_ext == '.png') ||
($jp_img_ext == '.gif')
) && ($jp_img_size < 10240)
)
{
//call the session file to get the current user
include("jobinpal_session.php");
$jp_usr_verified_ses=$_SESSION['jp_new_usr'];
//use the user session to create a folder in the 'usr/ceo' folder
//*** pattern for the foldername ***//
//------>>> JPCEO_ID_FILEID
//write a function to ouput from the database if...
//the user is a ceo,staff,student account
//function jp_check_usr_acc()
//{
$jp_usr_verified_ses=$_SESSION['jp_new_usr'];
//*****-----------------------------******//
//----------------------------------------//
//*****-----------------------------******//
include('jobinpal_db_config.php');
$jp_run=$jobinpal_db_lite->query("SELECT account_type,id FROM `jp_user` WHERE email='$jp_usr_verified_ses'");
//$jp_aff_rows=$jobinpal_db_lite->affected_rows;
while ($jp_result=$jp_run->fetch_array(MYSQLI_BOTH)) {
$jp_results[] = $jp_result;
}
foreach ($jp_results as $jp_result) {
$jp_res=$jp_result['account_type'];
$jp_res=strtoupper($jp_res);
$jp_res2=$jp_result['id'];
}
rmdir('../usr/ceo/JPCEO_20');
if (!is_dir("../usr/ceo/"."JP".$jp_res."_".$jp_res2))
{
mkdir("../usr/ceo/"."JP".$jp_res."_".$jp_res2, 0700);
$JP_new_usr_dir="../usr/ceo/"."JP".$jp_res."_".$jp_res2;
$JP_new_usr_dir="JP".$jp_res."_".$jp_res2;
//please i am having issues with the file uploads please put it stackoverflow
//for help...(?)
move_uploaded_file($jp_img_tmp_name,"`../usr/ceo/".$JP_new_usr_dir);
}
//$path=/*"http://".$domain.*/".."."/"."jobinpal"."/"."usr"."/"."ceo"."/".$JP_new_usr_dir;
//move_uploaded_file($jp_img_tmp_name,'../usr/ceo');
//}
//jp_check_usr_acc();
// move the uploaded file to the directory created for the user...
//move_uploaded_file($jp_img_tmp_name,$JP_new_usr_dir);
}
else
{
if (
/* file must be any of this format else bounce */
!
($jp_img_ext == '.jpg') ||
($jp_img_ext == '.png') ||
($jp_img_ext == '.gif')
)
{
echo "<h6 id='img_upload_err'>image must be this extensions (.jpg), (.png), (.gif)<h6>";
return false;
}
}
}
}
}
It looks like a typo:
move_uploaded_file($jp_img_tmp_name,"`../usr/ceo/".$JP_new_usr_dir);
^ is this supposed to be here?
You see the backtick in your error message as well but not in the directory you create and check for.
Related
I want to upload users sent files to a corresponding folder. This is my code. It does not upload to where I want.
$type=$_POST["type"];
$username_post=$_POST["username"];
$text=$_POST["text"];
$image=basename($_POST["image"]);
$page_number=$_POST["page_number"];
$video=basename($_POST["video"]);
$voice=basename($_POST["voice"]);
$title=$_POST["title"];
$dir=$username_post;
if( is_dir($dir) === false )
{
mkdir($dir,0666, true);
mkdir($dir.'/'.$title,0666, true);
}
else
{
mkdir($dir.'/'.$title,0666, true);
}
if( is_dir($dir.'/'.$title) === true )
{
$uploads_dir =$dir.'/'.$title;
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name,$uploads_dir.'/'.$name);
}
First of all you shouldn't create the directory from a user input post value.
Ex : if user enters some disallowed characters like "/*|" in your $_POST['username'], then you won't be able to create a directory with these values.
Check if you have permission to create directory in that folder.
Second check the $_FILES['file']['error'] == 0 or not. if not 0 that file has an error and can not be uploaded.
Use a "else condition" for last "if statement" to see if the "if statement" executes or not.
And also check if u have mentioned enctype="multipart/form-data" in your form tag.
I'm trying to make a new directory to handle profile pictures, but every time I upload the image, nothing happens as in the new directory is not created and there are no errors whatsoever. I already checked the Apache error log, but didn't notice any error pertaining to my recent code...
Here's a sample of my code
//profile image upload script
if (isset($_FILES['profile_pics'])) {
if (((#$_FILES["profile_pics"] ["type"] == "image/jpeg" || (#$_FILES["profile_pics"] ["type"] == "image/png") || (#$_FILES["profile_pics"] ["type"] == "image/gif")) && (#$_FILES["profile_pics"] ["size"] < 1048576)) ) // LESS THAN ONE MEGABYTE
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
mkdir("userdata/profile_pics/$rand_dir_name");
else {
}
}
}
You have multiple errors in your PHP, are you sure you did not see any FATAL errors in your apache logs?
Your if query does not close properly
Your else statement is missing a braket
You're suppressing any errors with # - remove them!
Use an absolute path instead of relative path to avoid errors in where you're creating the directory
Here's an example of how it should look:
// array of valid image types
$valid = array( 'image/jpeg', 'image/png', 'image/gif' );
if (isset($_FILES['profile_pics'])) {
// if the file type is in the valid array and the size is less than 1MB
if ( in_array($_FILES['profile_pics']['type'], $valid)
&& ($_FILES["profile_pics"]["size"] < 1048576) )
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
mkdir($_SERVER['DOCUMENT_ROOT'] . "/userdata/profile_pics/$rand_dir_name");
} else {
print( 'too big or not an image.' );
}
}
EDIT
You should also check the mkdir response.. like this:
$ok = mkdir($_SERVER['DOCUMENT_ROOT'] . "/userdata/profile_pics/$rand_dir_name");
if( !$ok )
{
print('error creating directory, check your permissions');
} else {
print('created directory successfully!');
}
Try to add permission.
mkdir("/path/", 0755);
I get this error message when I try to delete a directory:
"Unable to delete the file" using PHP Codeigniter's delete_dir('/path/to/my/dir/'). No additional info provided.
There is no problem with the ftp conn (works with everything else), no problem with the file permission (delete_file() works jsut fine)..no problem with the code, the path is also valid. I'm all out of ideas and open for suggestions.
You can use unlink() to remove php directory.
function removedir($dir1) {
if (is_dir($dir1)) {
//scan
$files = scandir($dir1);
//loop through all the files
foreach ($files as $file) {
if ($file != "." && $file != "..") {
if (filetype($dir1."/".$file) == "dir1")
rrmdir($dir1."/".$file);
else
unlink($dir1."/".$file);
}
}
reset($files);
rmdir($dir);
}
}
Okay this has baffled me. My script works in Mozilla but not IE. I get this error in IE:
Warning: move_uploaded_file(uploads/properties/yh96gapdna8amyhhmgcniskcvk9p0u37/) [function.move-uploaded-file]: failed to open stream: Is a directory in /homepages/19/d375499187/htdocs/sitename/include/session.php on line 602
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpJkiaF3' to 'uploads/properties/yh96gapdna8amyhhmgcniskcvk9p0u37/' in /homepages/19/d375499187/htdocs/sitename/include/session.php on line 602
my code at Session.php is:
function addPhoto($subphotoSize,$subphotoType,$subphotoTmpname,$subphotoDesc,$subfieldID,$subsessionid){
global $database, $form;
/* Get random string for directory name */
$randNum = $this->generateRandStr(10);
$maxFileSize = 2500000; // bytes (2 MB)
$filerootpath = PHOTOS_DIR.$subsessionid."/";
if($subphotoType == "image/png"){
$filename = $randNum.".png";
} else if ($subphotoType == "image/jpeg"){
$filename = $randNum.".jpg";
}
$fullURL = $filerootpath.$filename;
/* Image error checking */
$field = "photo";
if(!$subphotoTmpname){
$form->setError($field, "* No file selected");
} else {
if($subphotoSize > $maxFileSize) {
$form->setError($field, "* Your photo is above the maximum of ".$maxFileSize."Kb");
} else if (!is_dir($filerootpath)){
mkdir($filerootpath,0777);
chmod($filerootpath,0777);
}
move_uploaded_file($subphotoTmpname, "$fullURL");
}
/* Errors exist, have user correct them */
if($form->num_errors > 0){
return 1; //Errors with form
} else {
if($subfieldID == "1"){ // If the first field...
$is_main_photo = 1;
} else {
$is_main_photo = 0;
}
if(!$database->addNewPhoto($ownerID,$subphotoDesc,$fullURL,$userSession,$is_main_photo, $subsessionid)){
return 2; // Failed to add to database
}
}
return 0; // Success
}
It creates the folder no problem but doesnt do anything else.
Add a log message to see what's the value of $subphotoType. My guess is that when you upload the file from Internet Explorer it's neither image/png nor image/jpeg, in which case $filename will be empty because you don't have an else clause.
if($subphotoType == "image/png"){
$filename = $randNum.".png";
} else if ($subphotoType == "image/jpeg"){
$filename = $randNum.".jpg";
} else {
// No else! $filename will be empty.
}
If you tune your error reporting settings properly you should see a notice saying you are trying to use an undefined variable.
<?php
function rmdirr($dirname){
// Sanity check
$dirname = "TEST/";
if (!file_exists($dirname)) {
return false;
}
// Simple delete for a file
if (is_file($dirname)) {
return unlink($dirname);
}
// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == "." || $entry == "..") {
continue;
}
// Recurse
unlink("$dirname/$entry");
}
// Clean up
$dir->close();
return rmdir($dirname);
}
if (rmdirr($_GET['map'])){
echo "TEST FERo";
}
else{
echo "something went wrong.";
}
?>
Its working well and good. But if i need to delete the folder which contains some files and empty folder. In this case it delete all the files but not the empty folder. It throws an exception likeā¦
Warning: unlink(TEST//New Folder) [function.unlink]: Permission denied
in E:\Xampp\xampp\htdocs\delete_FILE\delete_FILE.php on line 23
Warning: rmdir(TEST/) [function.rmdir]: Directory not empty in
E:\Xampp\xampp\htdocs\delete_FILE\delete_FILE.php on line 28
What is the possible way to delete even the folder is empty.
Just use your rmdirr function recursively in the while loop instead of unlink;
function rmdirr($dirname){
// Sanity check
if (!file_exists($dirname)) { return false; }
// Simple delete for a file
if (is_file($dirname)) { return unlink($dirname); }
// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == "." || $entry == "..") { continue; }
// Recurse
rmdirr("$dirname/$entry");
}
// Clean up
$dir->close();
return rmdir($dirname);
}
This way it'll also take care of non-empty subfolders...
you have to make sure your webserver is able to delete those files. check the permissions.
You are deleting the files in 1 level only. Your code tries to delete the folder TEST//New Folder using unlink instead of rmdir. You have to check if it's a folder or not, then rmdir or unlink it.