So I've made a file upload site, and the uploading of the file works good. But the problem is, later on, when I want to edit the file (replace), the file doesn't update like it should.
I face a problem, when I upload a file with capital letters, I use "strtolower". When I want to replace that file with "Edit", the "strtolower" doesn't seem to work, and I can't access the file. The filename is shown in the table, but when I click "View", it doesn't get the file, tho, it gets uploaded to the server.
Here is the code for upload, and edit.php.:
if(isset($_POST['btn-upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$user_city = $_POST['user_city'];
$sifra = $_POST['sifra_izvoda'];
$folder="uploads/";
$new_size = $file_size/1024;
mb_internal_encoding('UTF-8');
$new_file_name = mb_strtolower($file);
$final_file = str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="INSERT INTO tbl_uploads(file,type,size,first_name,last_name,user_city,sifra) VALUES('$final_file','$file_type','$new_size','$first_name','$last_name','$user_city','$sifra')";
mysql_query($sql);
?>
<script>
alert('Izvod je dodan');
window.location.href='view.php?success';
</script>
<?php
}
Here the code when I want to edit the uploaded file:
if(isset($_GET['edit_id']))
{
$sql_query="SELECT file, type, size, first_name, last_name, user_city, sifra FROM tbl_uploads WHERE id=".$_GET['edit_id'];
$result_set=mysql_query($sql_query);
$fetched_row=mysql_fetch_array($result_set);
}
if(isset($_POST['btn-update']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$user_city = $_POST['user_city'];
$sifra = $_POST['sifra_izvoda'];
$folder="uploads/";
$new_size = $file_size/1024;
mb_internal_encoding('UTF-8');
$new_file_name = mb_strtolower($file);
$final_file = str_replace(' ','-',$new_file_name);
$tmp = move_uploaded_file($file_loc,$folder.$final_file);
$insert_file = ($_FILES['file']['name'])?"file='$file', type='$file_type', size='$file_size',":'';
$sql_query = "UPDATE tbl_uploads SET $insert_file first_name='$first_name', last_name='$last_name', user_city='$user_city', sifra='$sifra' WHERE id=".$_GET['edit_id'];
if(mysql_query($sql_query))
{
header("Location: view.php");
?>