Im learning php html and css and im creating a small site with account creation ofc.
None of the Php codes that i found works so if anyone can help me it will mean alot and thank you
CodePudding user response:
you can try this code
if($_POST['upload']){
$ekstensi_diperbolehkan = array('png','jpg');
$nama = $_FILES['file']['name'];
$x = explode('.', $nama);
$ekstensi = strtolower(end($x));
$ukuran = $_FILES['file']['size'];
$file_tmp = $_FILES['file']['tmp_name'];
if(in_array($ekstensi, $ekstensi_diperbolehkan) === true){
if($ukuran < 1044070){
move_uploaded_file($file_tmp, 'file/'.$nama);
$query = mysql_query("INSERT INTO upload VALUES(NULL, '$nama')");
if($query){
echo 'FILE BERHASIL DI UPLOAD';
}else{
echo 'GAGAL MENGUPLOAD GAMBAR';
}
}else{
echo 'UKURAN FILE TERLALU BESAR';
}
}else{
echo 'EKSTENSI FILE YANG DI UPLOAD TIDAK DI PERBOLEHKAN';
}
}
move_uploaded_file($file_tmp, 'file/'.$nama) you can save image from input user in that folder 'file'
CodePudding user response:
Making a File Upload form that checks supported file types
So first we need to make the form itself using HTML. Here is the code.
index.html
<html>
<head>
<title>Images</title>
</head>
<body>
<h1>Submit an Image to Arcade 70 Coding</h1>
<h3>only Image files are allowed.</h3>
<form action="submitted.php" enctype="multipart/form-data" method="post">
<input name="FileUpload" type="file" />
<br>
<input name="submit" type="submit" value="Submit" />
</form>
</body>
</html>
Notice
in the <FORM>
tag, include the enctype='multipart/form-data
or else it won't work. It also happened to me before.
Now lets continue to php. Name the file submitted.php. If you want to name it another name, change the file destination in the previous html code. More, it can copy the uploaded file to the admin's web server directory. Just replace /pathname/
in the line $FileUpload = '/pathname/'. $_FILES['FileUpload']['name'];
into your own specific path to folder. Also, it can show the user the uploaded file info. You don't need to include the TMP file path in the file info section. The File size were measured in bytes (b). Here is the code.
submitted.php
<html>
<head>
<title>Uploading File</title>
</head>
<body>
<h1>File Info</h1>
<?php
echo "<b>File Name: </b>". $_FILES['FileUpload']['name'] . "<br>";
echo "<b>File Type: </b>". $_FILES['FileUpload']['type'] . "<br>";
echo "<b>File Location: </b>". $_FILES['FileUpload']['tmp_name']. "<br>";
echo "<b>File Size: </b>". $_FILES['FileUpload']['size'];
echo " bytes (B)";
echo "<br><br>";
$FileSource = $_FILES['FileUpload']['tmp_name'];
$FileUpload = '/pathname/'. $_FILES['FileUpload']['name'];
// checks and submits file
if (isset($_POST["submit"])) {
if (($_FILES['FileUpload']['type'] == "image/png") or ($_FILES['FileUpload']['type'] == "image/jpg") or ($_FILES['FileUpload']['type'] == "image/jpeg") or ($_FILES['FileUpload']['type'] == "image/svg") or ($_FILES['FileUpload']['type'] == "image/bmp")
{
copy($FileSource,$FileUpload);
echo "<h1>Upload Complete!</h1>";
}
else
{
echo "<h3>Sorry, you can only upload image files.</h3>";
}
}
?>
</body>
</html>
Let me know if it works. For more info go here.