更新将不存储新的文件,但更新文本数据


Update Will Not Store New Files But Updates Text Data

我正在编写一个脚本来上传文件。文件被上传到一个文件夹中,路径被写入一个表中。一个表用于存储描述、名称等,另一个表用于存储路径。我可以上传文件,我可以编辑描述和名称,但我不能用新文件覆盖旧文件。我没有得到任何错误。该文件夹的权限设置为777。我已经适应了一个脚本,我用来上传图像,没有任何问题更新图像,所以我不明白为什么它不工作?这是编辑脚本。

非工作代码

<?php
require "authenticate.php";
error_reporting(E_ERROR);
$message = $_GET['message'];
function uploadfile($dir){
    if(!empty($_FILES)){
        $url ='';  
        // $file = ($_FILES["file"]["tmp_name"]);
        $allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "PNG", "GIF", "mp3");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        if ((($_FILES["file"]["type"] == "image/gif")
             || ($_FILES["file"]["type"] == "image/jpeg")
             || ($_FILES["file"]["type"] == "image/jpg")
             || ($_FILES["file"]["type"] == "image/pjpeg")
             || ($_FILES["file"]["type"] == "image/x-png")
             || ($_FILES["file"]["type"] == "image/png")
             || ($_FILES["file"]["type"] == "audio/mpeg")
             )
            && ($_FILES["file"]["size"] < 209715200)
            && in_array($extension, $allowedExts))
            {
                if ($_FILES["file"]["error"] > 0)
                    {
                        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                    }
                else
                    {
                        $path = $dir . $_FILES["file"]["name"];
                        {
                            move_uploaded_file($_FILES["file"]["tmp_name"],
                                               $dir . $_FILES["file"]["name"]);
                            $path = $dir . $_FILES["file"]["name"];
                        }
                    }
            }
        else
            {
                $message = "Wrong format";
            }
    }
    return $path;
}
//declare form field and form field error variables
$descriptionErr = $categoryErr = $titleErr = "";
$description = $category = $title = "";
//form field validation
function validate_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
if (isset($_POST['Submit']))
    {
        $has_errors = false;
        if(empty($_FILES["file"]["tmp_name"])){     
            $has_errors = true;
            $fileErr = "Missing Show!";
        }else{
            $dir = "audio/";
        }
        if (empty($_POST["title"])) {
            $has_errors = true;
            $titleErr = "Enter a title";
        }else {
            $title = validate_input($_POST["title"]);
        }
        if (empty($_POST["description"])) {
            $has_errors = true;
            $descriptionErr = "Enter a description";
        }else{
            $description = validate_input($_POST["description"]);
        }
        if (empty($_POST["category"])) {
            $has_errors = true;
            $categoryErr = "Enter a category";
        }else {
            $category = validate_input($_POST["category"]);
        }
        //write data into database table
        if (!$has_errors)
            {
                $Link = mysql_connect($Host, $User, $Password);
                $user = $_SESSION['UserName'];
                $path = uploadfile($dir);
                $Query = "INSERT INTO ccshowcontent VALUES ('','".mysql_escape_string($user)."','".mysql_escape_string($title)."','".mysql_escape_string($description)."',
        '".mysql_escape_string($category)."')";
                //pass id from form table into file table in order to link files to form data
                if(mysql_db_query ($DBName, $Query, $Link)) {
                    $formid = mysql_query("SELECT id FROM ccshowcontent ORDER BY id DESC LIMIT 1");
                    $formid = mysql_fetch_array($formid);
                    $Query = "INSERT INTO ccaudio VALUES ('{$formid[0]}','".mysql_escape_string($user)."','{$path}')";
                } else {
                    die("Query was: $Query. Error: ".mysql_error($Link));
                }
                if($sql = mysql_db_query ($DBName, $Query, $Link)) {
                    $message = "Show Saved";
                    header("Location: ccuploadshow.php?message=".urlencode($message));
                } else {
                    die("Query was: $Query. Error: ".mysql_error($Link));
                }
            }
    }
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles/all.css" />
<link rel="stylesheet" href="styles/formcomiccon.css" />
<link rel="stylesheet" href="styles/slideshow.css" />
<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
<link href='//fonts.googleapis.com/css?family=Economica:700,400italic' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Voltaire' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>Audio File Upload</title>
<meta name="Description" content="Audio File Upload" />
<meta name="Keywords" content="Audio File Upload" />
<script>
$(function(){
    $(document).click(function(){  
        $('.messagebox').hide();
    });
});
</script>
</head>
<body id="bodyform">
<p class="header"><a href="index.php">Audio File Upload</a></p>
<form action="ccuploadshow.php" method ="post" enctype="multipart/form-data" name="myForm">
<fieldset>
<div class="legendcreate">Upload</div>
<div class="audiocontainer">
<div class="audiocontainerinner">
    <?php if(isset($_GET['message']) && !empty($message)): ?>
    <div class="messagebox">
    <?php echo $message ?>
    </div>
    <?php endif; ?>
    <div><label class="labelcard">Title</label><input id="title" class="insetcard" name="title" type="text" placeholder="Title" value="<?PHP print $title ; ?>"/>
    <p class="errorcard"><?php echo $titleErr;?></p></div>
    <div><label class="labelcard">Category</label><input id="category" class="insetcard" name="category" type="text" placeholder="Category" value="<?PHP print $category ; ?>"/>
    <p class="errorcard"><?php echo $categoryErr;?></p></div>
    <div><textarea id="description" name="description" class="textareadescription" placeholder="Enter show description" value="<?PHP print $description ; ?>"></textarea>
    <p class="errordescription"><?php echo $descriptionErr;?></p></div>
<p class="errorfiles"><?php echo $fileErr;?>
<div class="uploadimgbtn"><p class="upload">Select Audio<input id="upfile" type="file" name="file" class="uploadbtn"/></p></div>
<div class="submit"><input name="Reset" type="reset" class="resetbtn" value="Reset"/></div>
<div class="submit"><input name="Submit" type="submit" class="submitbtn" value="Create"></div>
</div>
</div>
</fieldset>
</form>
</body>
</html>

我已经为那些有类似问题或需要脚本的人发布了解决方案。

<?php
require "authenticate.php";
error_reporting(E_ERROR);
$message = $_GET['message'];
function uploadfile($dir){
if(!empty($_FILES)){
$url ='';  
$file = ($_FILES["file"]["tmp_name"]);
$allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "PNG", "GIF", "mp3");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "audio/mpeg")
)
&& ($_FILES["file"]["size"] < 2009715200)
&& in_array($extension, $allowedExts))
    {
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
    $path = $dir . $_FILES["file"]["name"];
    {
    move_uploaded_file($_FILES["file"]["tmp_name"],
    $dir . $_FILES["file"]["name"]);
    $path = $dir . $_FILES["file"]["name"];
    }
    }
    }
else
    {
    $message = "Wrong format";
    }
}
return $path;
}
//declare form field and form field error variables
$titleErr = $descriptionErr = $categoryErr = "";
$title = $description = $category = "";
//form field validation
function validate_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    $data = mysql_real_escape_string($data);
    return $data;
}
if (isset($_POST['Submit']))
{
    $has_errors = false;
    if(empty($_FILES["file"]["tmp_name"])){     
    $path = mysql_real_escape_string($_POST['path']);
    }else{
    $dir = "audio/";
    }
    if (empty($_POST["title"])) {
    $has_errors = true;
    $titleErr = "Enter A Title";
    } else {
    $title = validate_input($_POST["title"]);
    }
    if (empty($_POST["description"])) {
    $has_errors = true;
    $descriptionErr = "Enter A Description";
    } else {
    $description = validate_input($_POST["description"]);
    }
    if (empty($_POST["category"])) {
    $has_errors = true;
    $categoryErr = "Enter A Category";
    } else {
    $category = validate_input($_POST["category"]);
    }
    if (!$has_errors)
    {
    $Link = mysql_connect($Host, $User, $Password);
    $user = $_SESSION['UserName'];
    if(empty($path)){
    $path = uploadfile($dir);
    }
//write edited data into tables matching logged in user with their data
$ccid = mysql_real_escape_string($_POST['ccid']);
    $Query = "UPDATE ccshowcontent SET title='$title', description='$description', category='$category' WHERE id='$ccid' AND '$user'='$user'";
    if(mysql_db_query ($DBName, $Query, $Link)) {
    $Query = "UPDATE ccaudio SET path='$path' WHERE id='$ccid'";
    } else {
    die("Query was: $Query. Error: ".mysql_error($Link));
    }
    if($sql = mysql_db_query ($DBName, $Query, $Link)) {
    $message = "Changes Saved";
    $current = $_GET['page'];
    header("Location: cceditshow.php?page=".$current."&message=".urlencode($message)); 
    } else {
    die("Query was: $Query. Error: ".mysql_error($Link));
}
}
}
//show logged in user their updated data
$user = $_SESSION['UserName'];
$result = mysql_query("SELECT * FROM ccshowcontent JOIN ccaudio USING (id) WHERE ccshowcontent.username = '$user'")
or die(mysql_error());
$count = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
$id[] = $row['id'];
$name[] = $row['name'];
$title[] = $row['title'];
$description[] = $row['description'];
$category[] = $row['category'];
$audio_paths[] = $row['path'];
}
//create counts and links for pagination
    if(empty($_GET['page'])){
    $i = 0;
    $current = $i + 1;
    }else{
    $i = $_GET['page'];
    $current = $i;
    $i = $i - 1;  
    }
    if($i == 0 && $count == 1){
    $prevlink = "";
    $next = $current + 1;
    $nextlink = "";
    }elseif($i == 0 && $count > 1){
    $prevlink = "";
    $next = $current + 1;
    $nextlink = "<a href='?page=$next'>Next</a>";
    }elseif($current > 0 && $current < $count){
    $prev = $current - 1;
    $next = $current + 1;
    $prevlink = "<a href='?page=$prev'>Previous</a>";
    $nextlink = "<a href='?page=$next'>Next</a>"; 
    }elseif($current == $count){
    $prev = $current - 1;
    $prevlink = "<a href='?page=$prev'>Previous</a>";
    $nextlink = "";
}
//delete form and image data when users clicks delete button
if (isset($_POST['Delete'])){
    $deletecard = $_POST['Delete'];
    mysql_query("DELETE FROM ccshowcontent WHERE id = '$deletecard'");
    mysql_query("DELETE FROM ccaudio WHERE id = '$deletecard'");
    mysql_query("ALTER TABLE ccshowcontent AUTO_INCREMENT = 1");  
    $message = 'Show Deleted';
    header("Location: cceditshow.php?&message=".urlencode($message));
}    
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles/all.css" />
<link rel="stylesheet" href="styles/formcomiccon.css" />
<link rel="stylesheet" href="styles/slideshow.css" />
<script type="text/javascript" src="javascript/jquery-2.1.1.js"></script>
<link href='//fonts.googleapis.com/css?family=Economica:700,400italic' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Voltaire' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>File Upload</title>
<meta name="Description" content="File Upload" />
<meta name="Keywords" content="File Upload" />
<script>
$(function(){
$(document).click(function(){  
$('.messagebox').hide();
});
});
</script>
</head>
<body id="bodyform">
<p class="header"><a href="index.php">File Upload</a></p>
<ul id="navigation">
<li><a href="#">Admin</a><ul>
<li><a href="cceditshow.php">Edit</a></li>
<li><a href="ccshows.php">Shows</a></li>
<li><a href="ccupload.php">Upload</a></li>
</ul></li>
<li><a href="register.php">Register</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
<br style="clear:left;"/>
<form action="" method ="post" enctype="multipart/form-data" name="editform">
<fieldset>
<div class="legendcreate">Edit</div>
<div class="audiocontainer">
<div class="audiocontainerinner">
    <?php if(isset($_GET['message']) && !empty($message)): ?>
    <div class="messagebox">
    <?php echo $message ?>
    </div>
    <?php endif; ?>
    <div><label class="labelcard">Title</label><input id="title" label="title" class="insetcard" name="title" type="text" placeholder="title" value="<?PHP print $title[$i] ; ?>"/><p class="errorcard"><?php echo $titleErr;?></p></div>
    <div><label class="labelcard">Description</label><input id="category" class="insetcard" name="category" type="text" placeholder="category" value="<?PHP print $category[$i] ; ?>"/>
    <p class="errorcard"><?php echo $categoryErr;?></p></div>
    <div><textarea id="description" name="description" class="textareadescription" placeholder="Enter character description"><?php
    $out = htmlspecialchars_decode($description[$i]); 
    $out = str_replace( ''n', '<br />', $out ); 
    echo $out; 
    ?></textarea>
<p class="errorbio"><?php echo $biographyErr;?></p></div>   
<input type="hidden" name="path" value="<?php echo $audio_paths[$i]; ?>"/>
<p class="errorimage"><?php echo $imageErr;?>
<div class="uploadimgbtn"><p class="upload">Select File<input id="upfile" type="file" name="file" class="uploadbtn"/></p></div>
<div class="submit"><input name="Submit" type="submit" class="savebtn" value="Save"/></div>
<input type="hidden" name="Delete" value="<?php echo $id[$i]; ?>">
<div class="delete"><input name="deletebtn" type="submit" class="deletebtn" value="Delete"/></div>
<input type="hidden" name="ccid" value="<?php echo $id[$i]; ?>"/>
</div>
</div>
</form>
</fieldset>
<div class="previouscardedit"><?php echo $prevlink; ?></a></div>
<div class="countpreviousedit"><?php if($prevlink != ""){ echo $prev."/".$count; } ?></div>
<div class="nextcardedit"><a class="tcg"><?php echo $nextlink; ?></a></div>
<div class="countnextedit"><?php if($nextlink != ""){ echo $next."/".$count; } ?></div>
<div class="currentcardedit"><?php echo "Show ".$current." of ".$count; ?></div>
<br style="clear:left;"/>
</body>
</html>