Jump to content


  • Content Count

  • Joined

  • Last visited

Everything posted by helloworld001

  1. I am using this for multi image upload. https://github.com/CreativeDream/jquery.filer Since it doesn't show example of how to use it with a database, I am having a bit of an issue. The following is my code. Couple things. 1. It won't insert into database with the "tokens". If I remove the token code, then it'll work. The same token works in other forms that I use. 2. Yes It inserts into the database. The issue is that it inserts only 1 file at a time and not all the selected files. <?php require_once ($_SERVER['DOCUMENT_ROOT'] . '/home/templates/header.php'); if(isset($_POST['submit'])) { if($_POST['token'] === $_SESSION['token']) { if(isset($_FILES['files'])){ $date_added = date('Y-m-d H:i:s'); // Setting up folder for images $user_dir = $_SERVER['DOCUMENT_ROOT'] .'/home/members/images/'.$db_userid.'/records/'; if(!is_dir($user_dir)){ mkdir($_SERVER['DOCUMENT_ROOT'] .'/home/members/images/'.$db_userid.'/records/', 0775, true); } else { $uploader = new Uploader(); $data = $uploader->upload($_FILES['files'], array( 'limit' => 10, //Maximum Limit of files. {null, Number} 'maxSize' => 10, //Maximum Size of files {null, Number(in MB's)} 'extensions' => null, //Whitelist for file extension. {null, Array(ex: array('jpg', 'png'))} 'required' => false, //Minimum one file is required for upload {Boolean} 'uploadDir' => '../uploads/', //Upload directory {String} 'title' => array('auto', 10), //New file name {null, String, Array} *please read documentation in README.md 'removeFiles' => true, //Enable file exclusion {Boolean(extra for jQuery.filer), String($_POST field name containing json data with file names)} 'perms' => null, //Uploaded file permisions {null, Number} 'onCheck' => null, //A callback function name to be called by checking a file for errors (must return an array) | ($file) | Callback 'onError' => null, //A callback function name to be called if an error occured (must return an array) | ($errors, $file) | Callback 'onSuccess' => null, //A callback function name to be called if all files were successfully uploaded | ($files, $metas) | Callback 'onUpload' => null, //A callback function name to be called if all files were successfully uploaded (must return an array) | ($file) | Callback 'onComplete' => null, //A callback function name to be called when upload is complete | ($file) | Callback 'onRemove' => 'onFilesRemoveCallback' //A callback function name to be called by removing files (must return an array) | ($removed_files) | Callback )); if($data['isComplete']){ $files = $data['data']; print_r($files); } if($data['hasErrors']){ $errors = $data['errors']; print_r($errors); } function onFilesRemoveCallback($removed_files){ foreach($removed_files as $key=>$value){ $file = '../uploads/' . $value; if(file_exists($file)){ unlink($file); } } return $removed_files; } for($i = 0; $i < count($_FILES['files']['name']); $i++) { $name = $_FILES['files']['name'][$i]; $temp = $_FILES['files']['tmp_name'][$i]; $ext = pathinfo($name, PATHINFO_EXTENSION); $upload = md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 )); $image_thumb_path = $user_dir . 'thumb_' . $upload . $ext; try { $insert_image = $db->prepare("INSERT INTO images(user_id, image_path, date_added) VALUES(:user_id, :image_path, :date_added)"); $insert_image->bindParam(':user_id', $db_userid); $insert_image->bindParam(':image_path', $image_thumb_path); $insert_image->bindParam(':date_added', $date_added); $result_image = $insert_image->execute(); if($result_image == false) { $error = 'There was a problem inserting images!'; } else { move_uploaded_file($temp, $image_thumb_path); $success = 'Your images has been saved.'; } } catch(Exception $e) { $error = die($e->getMessage()); } } } } } } ?> <h1>Upload Images</h1> <?php include_once ($dir_path . '/home/snippets/success-errors.php'); ?> <form action="" method="post" enctype="multipart/form-data"> <fieldset> <input type="file" multiple="multiple" name="files[]" id="input2"> <fieldset> <input type="hidden" name="token" value="<?php echo $_SESSION['token'] = md5(rand(time (), true)); ?>" /> <input type="submit" name="submit" class="red-btn" value="upload" /> </fieldset> </form> <?php require_once ($_SERVER['DOCUMENT_ROOT'] . '/home/templates/footer.php');
  2. In my case, thumbnails mean a resized image. I basically upload and resize the image(with new resolution and smaller bytes). I know now that I don't have to keep the original image in the database and directory folder. The resized image(thumbnail) is all I need.
  3. I should mention that the resized image(thumbnail) is still fairly large enough in terms of resolution, but lower in file size. And that's all I'll be using throhghout the site. Since I have no use for the original image, I guess it's best to not save it. It will definitly save up space in the directory.
  4. Say I am uploading an image that gets resized. The resized image is a thumb. I have it's file path saved in the database and the image itself saved in a folder. Originally I was saving both to the database and folder. But now that I think about it, do I have to save the orginal image? Wouldn't I be saving up a lot of space if I only save the thumb image? What do you think?
  5. Understood. I will remove the database sessions completely and follow your advice on seeings users who are online. Thanks.
  6. I see. Now my question is, do I HAVE to store the user sessions in the database?
  7. Let me clarify. When a user is logged in, a user's session is created. I took an extra step and decided to insert that session into the database table so that I can keep track of the users who are logged in. Yes every time I close the browser window, the user session on server side gets destroyed. But the user session in the database remains there. That only gets deleted when the logged in user signs out. Now based on "QuickOldCar"s suggestion, I now use expiry time. It works. Basically every time a user logins in, I insert the session data(including current time + add minutes) into the table. Like this. $time_created = date("Y-m-d H:i",strtotime("+10 minutes")); I then check that time against the current time on the php page. If it's less than current time, I delete the session from the table. So all in all, it works now.
  8. I have a simple user login where it inserts the user session into database like this (id, user_id, hash). It works fine. I can delete the session from the database when the user logs out. Now I realized something. When I close the browser window, it'll destroy the session(log out the user) as intended but it won't delete the session from the database. I was wondering if there is a way to do that?
  9. You are right. I have it working finally. Though there is a slight mistake in your code. The "home" directory has to be included for it to work. Here is the updated line. It'll work, no matter which sub folder you are calling it from. require_once ($_SERVER['DOCUMENT_ROOT'] . '/home/core/init.php');
  10. Yes that does work. Now one other thing that I've noticed. I have couple more files that I "require_once" in the "init.php" file and as well as include some snippets in the header. This poses an issue. If I make the file paths so that they show up and work properly for the files in "members" folder, those same file paths won't work for the files in the "home" directory. There has to be a cleaner way to do this no?
  11. Still doesn't work. I get this error. Warning: require_once(core/init.php): failed to open stream: No such file or directory in C:\xampp\htdocs\other\template\header.php on line 2 Fatal error: require_once(): Failed opening required 'core/init.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\other\template\header.php on line 2 I just noticed something. In my header.php file, I also require another file. Like this. header.php require_once 'core/init.php'; Here's the updated directory with the core folder. home(main directory) /core /init.php /template /header.php /members /private /settings.php So perhaps it's not working because I am calling 2 files from 2 different locations at the same time?
  12. None of the examples you provided work. This is the type of error I get. Warning: require_once(C:\xampp\htdocs\other\members\private/template/header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\other\members\private\settings.php on line 3 Here's a better look at the directory structure. home(main directory) /template /header.php /members /private /settings.php
  13. I am trying to include a file from another directory and so far I keep getting errors such as "failed to open stream: No such file or directory in...". Here's my directory setup. home/ /template/header.php /members/private/settings.php All I want to do is include "header.php" file on my "settings.php" page. Here's an example. settings.php <?php $dir = dirname(__FILE__); require_once $dir.'/template/header.php'; Can you tell me what's wrong with it? It's not working.
  14. Ahh yes I see what you did there. It works. The only thing is that it doesn't work with "mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)". But if I try "uniqid()", then it'll work. Is there anything else I can use to make it more secure?
  15. I need new pair of eyes to look at this and tell me what's wrong with it. All I am trying to do is have a simple form that submits data to database. It works without the "token". With the token code added, it won't let process. I even did var_dump and the session and the $_post code doesn't match. Here's the code. Btw, session_start() and the database connection are in the init.php file. <?php require_once 'init.php'; $token = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM); $_SESSION['token'] = $token; if(isset($_POST['register'], $_POST['token'])) { if($_POST['token'] === $_SESSION['token']) { $email = trim($_POST['email']); $password = trim($_POST['password']); if(empty($email)) { $error = 'Email is required!'; } else if(empty($password)) { $error = 'Password is required!'; } else if(strlen($password) < 6) { $error = 'Password must be at least 6 characters long!'; } else { $findUser = $db->prepare("SELECT email FROM users WHERE email = :email"); $findUser->bindParam(':email', $email); $findUser->execute(); $resultFind = $findUser->fetchAll(PDO::FETCH_ASSOC); if(count($resultFind) > 0) { $error = 'The email already exists! Please try a different email!'; } else { //Hash the password as we do NOT want to store our passwords in plain text. $passwordHash = password_hash($passward, PASSWORD_BCRYPT, array("cost" => 12)); $insertUser = $db->prepare("INSERT INTO users(email, password) VALUES(:email, :password)"); $insertUser->bindParam(':email', $email); $insertUser->bindParam(':password', $passwordHash); $resultInsert = $insertUser->execute(); if($resultInsert == false) { $error = 'There was a problem creating your account. Please try again later!'; } else { $success = 'Your account has been created.'; unset($_SESSION['token']); } } } } else { $error = 'The tokens do not match!'; } } ?> <h1>Sign up</h1> <form action="" method="post"> <fieldset> <input type="email" name="email" value="<?php echo $email; ?>" placeholder="Email" /> </fieldset> <fieldset> <input type="password" name="password" placeholder="Password" /> </fieldset> <fieldset> <input type="hidden" name="token" value="<?php echo $token; ?>" /> <input type="submit" name="register" value="Sign up" /> </fieldset> </form>
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.