How to make a button, upload image to c9 folder? - php

I was wondering if it was possible to make a button in an HTML page or PHP that has the function to upload an image to your c9 folder instead "img" to your database(phpMyAdmin).
Once it is uploaded you can also display the image you just uploaded. But i think I can sort this one out like:
<html>
<img src='photo/'/>
</html>
//or
<?php echo "<img src='photo/'/>"?>
What I want to do with this is like filling a form in and have a photo with it as extra information.
edit:
I tried this code:
$target_dir = "photo/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
Once i clicked the button to upload it, it did not save the file in the folder.
edit:
It works now, had to do the php.ini setup in c9.
P.S: I just started programming and would like to learn. If I make my question unclear please tell me how I can improve my question to be more accurate. :D

Refer this and this is a simple question,before ask the this type of question please search and study from internet.
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>

Related

Upload file trouble in PHP

I have made an upload image a couple times before and I never have problems. And I just copy the script and it worked fine, but after I copy I don't know why it keep shows that I'm not using the right extension. Can you spot the mistake?
This is the form script:
<tr height="30">
<td> </td>
<td align="right">Gambar Barang </td>
<td><input type="file" name="gambar" /></td>
</tr>
and its the action,
$errors= array();
$uploaddir = 'images/';
$uploadfile = $uploaddir . basename($_FILES['gambar']['name']);
$gambar = $_FILES['gambar']['name'];
$gambar_size = $_FILES['gambar']['size'];
$gambar_type = $_FILES['gambar']['type'];
$gambar_tmp = $_FILES['gambar']['tmp_name'];
$gambar_ext=strtolower(end(explode('.',$_FILES['gambar']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($gambar_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($gambar_size > 4097152){
$errors[]='File size must be excately 4 MB';
}
if(empty($errors)==true){
copy($_FILES['gambar']['tmp_name'], $uploadfile);
}else{
print_r($errors);
}
There are several things that could cause file upload to fail:
does the upload form have enctype="multipart/form-data"?
is file_uploads = On in php.ini?
what is the max post/upload limit in php.ini?
did you check the permissions for the upload folder?
Your code works fine, just make sure your form uses:
method='post'
and(as already mentioned by Reto) - enctype="multipart/form-data"
i.e. something along the lines of:
<form method='post' action='#' enctype='multipart/form-data'>

Image not uploading to web server

I am having some difficulty uploading an image to a folder on my web server. Here's my code:
HTML:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Upload Image" name="upload">
</form>
PHP:
<?php
if (isset($_POST["upload"])) {
$name = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$temp = $_FILES["image"]["tmp_name"];
$error = $_FILES["image"]["error"];
$target_file = "/profiles/images/$name";
move_uploaded_file($temp, $target_file);
}
?>
I've tried echoing out the file name ($name), but it doesn't return anything at all. It's blank for some reason. This is when I try to upload an image. When I echo $target_file, I get this "/profiles/images/", the $name part is not included for some reason.
Check your max upload file size. If in the settings the size is smaller than the file you are trying to upload, nothing is send.
This is happens in those cases when we try to upload very heavy size image, Please try to check your maximum file size, and please for testing try to upload maximum 100kb size of pic through this code. Hope this will work.

My PHP script is giving me very slow upload speed ~400kbps on my localhost using XAMPP server

This is my form :
<form name="fileup" action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" title=" "/>
<input style="position:absolute; float:right;"type="submit" value="Upload"/>
</form>
And that's the PHP:
session_start();
date_default_timezone_set('Asia/Kolkata');
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
if($file_name=="")
echo '<script>
alert("No File Selected");
window.location.href="index.php";
</script>';
else if(empty($errors)==true and !$file_size==0){
move_uploaded_file($file_tmp,"uploads/".$file_name);
While text (using another form) and small files have no problem, large files ~500M uploaded from clients on the same WLAN start at ~1Mbps and then commence at ~400kbps. However, uploads from my own computer happen almost instantaneously. I am a beginner and am using XAMPP server on my windows machine.
These are the modifications i made to the php.ini file:
> max_execution_time=120
> post_max_size=16000M
> upload_max_filesize=8000M
Please Help.
Will shifting to another protocol like FTP help?

Uploading Image to server using direct link and without form in PHP

This code allow me to choose an Image and upload it:
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_tmp =$_FILES['image']['tmp_name'];
if(empty($errors)==true){
move_uploaded_file($file_tmp,"uploads/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
I want to upload the image using a direct link to the target image and without using a form or file selector..
so the image could be uploaded as soon as loading the php file

How to upload images to a domain at webhost using php?

What should I do if I want to upload an image to a file system such as a folder in my domain? The thing is, I've tried uploading image on a localhost and it works fine but when i did it in my domain it doesn't work anymore. Don't really know what's the problem here. any help will do! Thanks anyway.
<form action="adminUploadTry.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" >
<input type="submit" name="submit" value="Upload" >
</form>
<?php
if(isset($_POST['submit'])){
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];
if($image_name==''){
echo "You forgot to select an image. Please choose one!";
exit();
}
else
move_uploaded_file(image_tmp_name, "/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name");
echo "Image Succesfully Uploaded!";
echo "<img src='/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name'>";
}
?>
by the way, the problem here is that after i uploaded an image it will say "successfully uploaded" but when I check my folder the image was not saved.
May be you forget $ sign in image_tmp_name at
move_uploaded_file($image_tmp_name, "/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name");
See if you need to increase the value of upload_max_filesize and post_max_size in your php.ini :
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

Categories