Jump to content

Search the Community

Showing results for tags 'upload'.

More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • Welcome to PHP Freaks
    • Announcements
    • Introductions
  • PHP Coding
    • PHP Coding Help
    • Regex Help
    • Third Party Scripts
    • FAQ/Code Snippet Repository
  • SQL / Database
    • MySQL Help
    • PostgreSQL
    • Microsoft SQL - MSSQL
    • Other RDBMS and SQL dialects
  • Client Side
    • HTML Help
    • CSS Help
    • Javascript Help
    • Other
  • Applications and Frameworks
    • Applications
    • Frameworks
    • Other Libraries
  • Web Server Administration
    • PHP Installation and Configuration
    • Linux
    • Apache HTTP Server
    • Microsoft IIS
    • Other Web Server Software
  • Other
    • Application Design
    • Other Programming Languages
    • Editor Help (Dreamweaver, Zend, etc)
    • Website Critique
    • Beta Test Your Stuff!
  • Freelance, Contracts, Employment, etc.
    • Services Offered
    • Job Offerings
  • General Discussion
    • PHPFreaks.com Website Feedback
    • Miscellaneous

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start





Website URL








Donation Link

Found 9 results

  1. I am trying out a new script for image upload and resize using ajax method. All the ones I've found so far process the php file through the form action="". Since I am inserting other data into the database and calling the other php code directly on the same page as a the html form, I would like to know if there is another way I can run that specific image upload php code through ajax. This is one the scripts I have looked at . http://www.sanwebe.com/2012/05/ajax-image-upload-and-resize-with-jquery-and-php This is what their html form looks like. <form action="processupload.php" method="post" enctype="multipart/form-data" id="MyUploadForm"> <input name="image_file" id="imageInput" type="file" /> <input type="submit" id="submit-btn" value="Upload" /> <img src="images/ajax-loader.gif" id="loading-img" style="display:none;" alt="Please Wait"/> </form> <div id="output"></div> I would like to process the "processupload.php" above through the ajax code below and leave the form action="" empty, as I am running other php code on the same page to insert other data as well. How would you do that? <script> $(document).ready(function() { var options = { target: '#output', // target element(s) to be updated with server response beforeSubmit: beforeSubmit, // pre-submit callback success: afterSuccess, // post-submit callback resetForm: true // reset the form after successful submit }; $('#MyUploadForm').submit(function() { $(this).ajaxSubmit(options); // always return false to prevent standard browser submit and page navigation return false; }); }); function afterSuccess() { $('#submit-btn').show(); //hide submit button $('#loading-img').hide(); //hide submit button } //function to check file size before uploading. function beforeSubmit(){ //check whether browser fully supports all File API if (window.File && window.FileReader && window.FileList && window.Blob) { if( !$('#imageInput').val()) //check empty input filed { $("#output").html("Are you kidding me?"); return false } var fsize = $('#imageInput')[0].files[0].size; //get file size var ftype = $('#imageInput')[0].files[0].type; // get file type //allow only valid image file types switch(ftype) { case 'image/png': case 'image/gif': case 'image/jpeg': case 'image/pjpeg': break; default: $("#output").html("<b>"+ftype+"</b> Unsupported file type!"); return false } //Allowed file size is less than 1 MB (1048576) if(fsize>1048576) { $("#output").html("<b>"+bytesToSize(fsize) +"</b> Too big Image file! <br />Please reduce the size of your photo using an image editor."); return false } $('#submit-btn').hide(); //hide submit button $('#loading-img').show(); //hide submit button $("#output").html(""); } else { //Output error to older browsers that do not support HTML5 File API $("#output").html("Please upgrade your browser, because your current browser lacks some new features we need!"); return false; } } //function to format bites bit.ly/19yoIPO function bytesToSize(bytes) { var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; if (bytes == 0) return '0 Bytes'; var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; } </script>
  2. Hi there guys, I've a little problem with inserting a file name into a database table. I can't see wich is the problem. The code is bellow and i think the problem is at INSERT INTO part. <?php $path = "./cv/"; $valid_formats = array("doc", "pdf"); if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") { $name = $_FILES['photoimg']['name']; $size = $_FILES['photoimg']['size']; if(strlen($name)) { list($txt, $ext) = explode(".", $name); if(in_array($ext,$valid_formats)) { if($size<(20480*20480)) // Image size max 20 MB { $actual_image_name = time().$id.".".$ext; $tmp = $_FILES['photoimg']['tmp_name']; if(move_uploaded_file($tmp, $path.$actual_image_name)) { mysqli_query($mysqli,"INSERT INTO formular_client (client_cv = '$actual_image_name')"); } else echo "failed"; } else echo "Image file size max 20 MB"; } else echo "Invalid file format.."; } } ?> <input type="file" name="photoimg" id="photoimg" />
  3. Hi guys and Gals, This has my head wrecked to be honest. I am trying to upload an image to a directory, which is working. However, I also want to put the file name into MySQL. This will work if the image upload script is removed. With the script enabled, the file uploads but I get "Undefined index: userPic" from the following line: $userPic = mysqli_real_escape_string($mysqli, $_POST['userPic']); Here is the complete code: if(isset($_POST['Submit'])){//if the submit button is clicked $company_name = mysqli_real_escape_string($mysqli, $_POST['company_name']); $company_abn = mysqli_real_escape_string($mysqli, $_POST['company_abn']); $company_email = mysqli_real_escape_string($mysqli, $_POST['company_email']); $address = mysqli_real_escape_string($mysqli, $_POST['address']); $company_phone = mysqli_real_escape_string($mysqli, $_POST['company_phone']); $company_slogan = mysqli_real_escape_string($mysqli, $_POST['company_slogan']); $userPic = mysqli_real_escape_string($mysqli, $_POST['userPic']); // Upload Image if (isset($_FILES["userPic"]["name"])) { $name = $_FILES["userPic"]["name"]; $tmp_name = $_FILES['userPic']['tmp_name']; $error = $_FILES['userPic']['error']; if (!empty($name)) { $location = 'uploads/'; if (move_uploaded_file($tmp_name, $location.$name)){ echo 'Uploaded'; } } else { echo 'please choose a file'; } } $sql="UPDATE company_settings SET company_name='$company_name', company_slogan='$company_slogan', company_abn='$company_abn', company_email='$company_email', address='$address', company_phone='$company_phone', userPic='$userPic'"; $mysqli->query($sql) or die("Cannot update");//update or error } Has anyone got any ideas where I am going wrong (besides not using PDO) and how I can solve it? Thanks in advance.
  4. The image uploads fine but I get this weird gibberish code after I submit. It goes away after i refresh the page. JFIF>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality C $.' ",#(7),01444'9=82<.342C 2!!22222222222222222222222222222222222222222222222222" }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr $4%&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz?((((((((((((((((((((((((((((((((((((((((((((((((((((( Here's my code. if(isset($_POST['submit'])) { $errors = array(); $db->beginTransaction(); if(isset($_FILES['image'])) { if(!empty($_FILES['image']['name'])) { $name = $_FILES['image']['name']; $temp = $_FILES['image']['tmp_name']; $type = $_FILES['image']['type']; $size = $_FILES['image']['size']; $ext = pathinfo($name, PATHINFO_EXTENSION); $size2 = getimagesize($temp); $width = $size2[0]; $height = $size2[1]; $upload = md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 )); // Restrictions for uploading $maxwidth = 1500; $maxheight = 1500; $allowed = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif'); // Recognizing the extension switch($type){ // Image/Jpeg case 'image/jpeg': $ext= '.jpeg'; break; // Image/Jpg case 'image/jpg': $ext= '.jpg'; break; // Image/png case 'image/png': $ext= '.png'; break; // Image/gif case 'image/gif': $ext= '.gif'; break; } // check if extension is allowed. if(in_array($type, $allowed)) { // Checking if the resolution is FULLHD or under this resolution. if($width <= $maxwidth && $height <= $maxheight) { if ($size <= 5242880) { $admin_dir = "images/"; if(!is_dir($admin_dir)){ mkdir($admin_dir, 0775, true); } // upload variables $path = $admin_dir . $upload . $ext; // Resizing according to extension. switch ($type) { // Image/Jpeg case 'image/jpeg'; $img = $temp; $thumb = imagecreatetruecolor($width, $height); imagejpeg($thumb); imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); imagealphablending($thumb, false); imagesavealpha($thumb, true); break; // Image/Jpg case 'image/jpg'; $img = $temp; $thumb = imagecreatetruecolor($width, $height); imagejpeg($thumb); imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); imagealphablending($thumb, false); imagesavealpha($thumb, true); break; // Image/png case 'image/png'; $img = $temp; $thumb = imagecreatetruecolor($width, $height); imagepng($thumb); imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); imagealphablending($thumb, false); imagesavealpha($thumb, true); break; // Image/gif case 'image/gif'; $img = $temp; $thumb = imagecreatetruecolor($width, $height); imagegif($thumb); imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); imagealphablending($thumb, false); imagesavealpha($thumb, true); break; } $insert_date = date('Y-m-d H:i:s'); $insert = $db->prepare("INSERT INTO images(path, date_added) VALUES(:path, :date_added)"); $insert->bindParam(':path', $path); $insert->bindParam(':date_added', $insert_date); $result_insert = $insert->execute(); if($result_insert == false) { $errors[] = 'Profile photo could not be uploaded.'; } if(empty($errors)) { $db->commit(); move_uploaded_file($temp, $path); $success = 'Profile photo has been updated.'; } else { $db->rollBack(); } } else { $errors[] = 'The image size is bigger than 5mb.'; } } else { $errors[] = 'The image resolution exceeds the limit of 1500px by 1500px.'; } } else { $errors[] = 'You have uploaded a forbidden extension.'; } } else { $errors[] = 'An image is required.'; } } } ?> <form id="upload-form" action="" method="post" enctype="multipart/form-data"> <label>175px by 175px</label> <input type="file" name="image" /> <input type="submit" id="upload-submit" value="Upload Image" name="submit"> </form>
  5. I am using this script for image uploads. https://www.w3schools.com/php/php_file_upload.asp I noticed that with some of the image uploads, I would get the error "Sorry, only JPG, JPEG, PNG & GIF files are allowed." The images i upload are one of the file types listed above. So I am wondering why i would get an error for some images but not others despite all them of being the same file types? Can you tell why judging from the script?
  6. 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?
  7. 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');
  8. I am trying out a new script that lets me resize an image before uploading. It is based on this script. http://www.w3bees.com/2013/03/resize-image-while-upload-using-php.html Basically what happens is, it resizes and makes 3 thumbnails and puts them in their relative folder. That works. The part that's giving me the problem is when inserting it into the database. Error says the "image_path" cannot be null. Since it's creating an array of 3 different thumbnails, should I create 2 more fields in the database tabel to account for that? If so, how would I insert the 3 different thumbnail paths into the query? What would it look like? Below is my code. <?php $db_userid = intval($row['user_id']); $get_item_id = intval($row['item_id']); // settings $max_file_size = 5242880; // 5mb $valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // thumbnail sizes $sizes = array(100 => 100, 150 => 150, 250 => 250); // dir paths $target_dir = 'images/'.$db_userid.'/items/'.$get_item_id.'/'; if ($_SERVER['REQUEST_METHOD'] == 'POST' AND isset($_FILES['image'])) { if(!empty($_FILES['image']['name'])) { if($_FILES['image']['size'] < $max_file_size ){ // get file extension $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION)); if(in_array($ext, $valid_exts)) { function resize($width, $height){ global $db_userid; global $get_item_id; /* Get original image x y*/ list($w, $h) = getimagesize($_FILES['image']['tmp_name']); /* calculate new image size with ratio */ $ratio = max($width/$w, $height/$h); $h = ceil($height / $ratio); $x = ($w - $width / $ratio) / 2; $w = ceil($width / $ratio); /* new file name */ $path = 'images/'.$db_userid.'/items/'.$get_item_id.'/'.$width.'x'.$height.'_'.$_FILES['image']['name']; /* read binary data from image file */ $imgString = file_get_contents($_FILES['image']['tmp_name']); /* create image from string */ $image = imagecreatefromstring($imgString); $tmp = imagecreatetruecolor($width, $height); imagecopyresampled($tmp, $image, 0, 0, $x, 0, $width, $height, $w, $h); /* Save image */ switch ($_FILES['image']['type']) { case 'image/jpeg': imagejpeg($tmp, $path, 100); break; case 'image/png': imagepng($tmp, $path, 0); break; case 'image/gif': imagegif($tmp, $path); break; default: exit; break; } return $path; /* cleanup memory */ imagedestroy($image); imagedestroy($tmp); } /* resize image */ foreach($sizes as $w => $h) { if(!is_dir($target_dir)){ mkdir($target_dir, 0775, true); } $files[] = resize($w, $h); } $insert_image = $db->prepare("INSERT INTO images(user_id, item_id, image_path, date_added) VALUES(:user_id, :item_id, :image_path, :date_added)"); $insert_image->bindParam(':user_id', $db_userid); $insert_image->bindParam(':item_id', $get_item_id); $insert_image->bindParam(':image_path', $path); $insert_image->bindParam(':date_added', $item_date_added); if(!$insert_image->execute()) { $errors[] = 'There was a problem uploading the image!'; } else { if(empty($errors)) { $db->commit(); $success = 'Your item has been saved.'; } else { $db->rollBack(); } } } else { $errors[] = 'Unsupported file'; } } else{ $errors[] = 'Please upload image smaller than 5mb'; } } else { $errors[] = 'An image is required!'; } }
  9. Hello all, I dont know why I'm getting this error with calculating the duration for the video, everything works fine with uploading, file is going in my database and folder, the duration of my video is ok. ​http://prnt.sc/e59i4f here is my code: // video duration ob_start(); passthru("ffmpeg -i $tmp 2>&1"); $duration = ob_get_contents(); ob_end_clean(); $search='/Duration: (.*?)[.]/'; $duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE); //error is on this line botom $duration = $matches[1][0]; ​//error is on this line botom list($hours, $mins, $secs) = split('[:]', $duration); //echo "Hours: ".$hours." Minutes: ".$mins." Seconds: ".$secs; $duration=$hours.':'.$mins.':'.$secs;
  • 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.